Batch - redirect program output to current console - batch-file

Lets say this is test.bat
START cmd /c "D:\myprogram.exe" arg0 arg1 ^>out.txt
pause
This works fine, but I want to show the output of myprogram.exe inside the test.bat console, instead of creating a text file for it. The problem is that myprogram.exe creates a new console, finishes and closes the console window before I can see the output and so far I can only write it to a text file. How can I redirect the output to test.bat's console window instead?

According to the comments, it appears that the following is all I needed:
#"D:\myprogram.exe" arg0 arg1

From help
C:\WINDOWS\system32>start /?
Starts a separate window to run a specified program or command.
…
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
parameters These are the parameters passed to the command/program.
So to recap. Start is for starting programs in unusual ways.

Related

Bat, How to prevent environment variables leaking into parent process

I have a bat file similiar to this
rem build.bat
rem add visual studio paths to env
call "%PROGRAMFILES(X86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
cl.exe blabla
After running it a couple of times I get the error The input line is too long. because vcvarsall.bat seems to append to the path variables every time I invoke the script
I invoke the script with build.bat
I thought that the environment variables should not be saved between runs. Is there any way to make the variables not leak into the calling cmd shell?
You should localize the environment of your batch file by putting setlocal at the beginning and endlocal at the end (the latter may be omitted as it executes implicitly on exiting the script).
You could start the build.bat in a child process.
cmd /c build.bat
Variables in the parent process aren't affected by a child process.
As far as I understand, environment variables don't get saved between runs, which is correct, but a "run" in this case is not the "run" of the batchfile, but of the command prompt in which you run the batchfile.
In my opinion, you have opened a command prompt. In there you launch the "build.bat" file, over and over again.
However, if you would launch the batchfile from outside of a command prompt (like double-clicking from the Windows explorer), this problem should not appear.

Start a batch file and close Cmd once it is finished

I have my application spread over multiple directories, each containing a part (e.g. web frontend, mobile application, administration, middleware, backend, ...).
In each of the directories I have one part, and a file compile.cmd which compiles that part and looks roughly like this:
#ECHO OFF
compiler prepare thisPart
compiler compile thisPart
copy resultingFile1 ../bundleDirectory
...
copy resultingFileN ../bundleDirectory
pause
The "pause" is so I can check the compiler output, whether compile failed and which error messages occurred, and then close the window with a single keystroke (mostly, space key).
I now want to have a batch file that calls all these batch files for different application parts in parallel, so I guess I have to open a new shell window for each.
So I wrote CompileAllParts.cmd like this:
cd part1
start "Compile part 1" compile.cmd
cd ../part2
start "Compile part 2" compile.cmd
cd ../part3
start "Compile part 3" compile.cmd
Positive is that I can influence the window title, but the drawback is that new Cmds are spawned which do not automatically close.
This is also part of the documentation of start:
If command is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.
Is there a hidden parameter to explicitly disable this behaviour?
combine start with cmd. First to set title and working folder, second to use /C and execute the command:
start "Compile part 1" /d "part1" cmd /c "d:\proper folder\compile.cmd"

exit batch processing even if calling program still running

I think it's a simple question, but I don't realize its solution.
I have a batch file that calls an EXE program. It's working fine, but I'd like to close the CMD window without having to click on it.
It's like this:
#echo off
[...]
call EXEprogram.exe
[...]
exit
I want the CMD window vanishes, even if the EXEprogram is still running, but the control doesn't return to the batch file, to execute the EXIT command. I tested using CALL command calling another batch file that calls EXEprogram and also, don't using the CALL command, but result is the same: CMD window shows that EXEprogram is still running and the control doesn't come back to execute EXIT command.
Ideas?
Use the start command instead of call:
start EXEprogram.exe
Type start /? for further details.

Start a Java program from batch file and exit without waiting

I have a small Windows batch script that is executed from within a game application (the user experience is important here) that is called to start a Java program among other things. With the command javaw I hide console output from the user of the game to improve experience, however, the console window remains open from the batch file execution and doesn't close until the Java code is finished executing.
The first solution I tried was to use the start command, but I am unable to pass any parameters to the java program and it fails accordingly.
start javaw -jar jar-file-here.jar args...
How should I write a batch file that will start a Java program with the correct variables, and close without waiting for Java to close?
Edit:
My parameters contain quotes ( " ), so I am unable to contain them all in quotes without confusing the command.
The first argument accepts a window title. Here is the command with the adjustment made:
start "Window Title Here" javaw -jar jar-file-here.jar args...

Run batch files sequentially

I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.

Resources