exit batch processing even if calling program still running - batch-file

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.

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.

Batch - redirect program output to current console

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.

How to create a .bat file that starts a customized CMD, in which it run a script?

I want to make a automated program in .bat. The program needs to run a command. However, the command must be run from a custom CMD.
If I open a regular CMD, the commands that I will do:
C:\Hardware\bin\StartCustomCMD.bat init (This is the first thing I will type. It starts the custom CMD.)
bb autobuild (This is the second thing that I will type. The command goes into the custom CMD)
You can probably tell that I did not write these scripts. I am trying to set this up in Windows Scheduler so that the script gets run automatically every day. Any help on how can I do that?
Thanks.
Make yourself a new batch file, and embed the other things into it, and then run that.
#echo off
call C:\Hardware\bin\StartCustomCMD.bat
bb autobuild
If bb it itself a batch file, then use call on it also. What call does is execute another batch file, and then continue processing. If you don't use call, when you run one batch file from another, the latter 'takes over' and the caller does not continue.
To do this you can use the timeout and goto command. Timeout waits a period of time in seconds but it can be skipped by pressing any key while cmd is open at the top layer. If you can see cmd press its icon on the desktop until you can not see it. Then using the goto command you can go to the top line. So here would be your script:
:start
C:\Hardware\bin\StartCustomCMD.bat init
bb autobuild
timeout 86400
goto start
You already know what the first two commands do, but timeout 86400 waits exactly one day then the goto start command goes to the first command so that gets repeated. If you need to add any more commands then put them above the timeout 86400 command.

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.

Batch script stops after first call to other batch script

I'm attempting to execute a batch script that currently looks like this:
D:
cd D:My Documents\FtpSolution\Test
getftp.bat
call delimconvert.exe
call convert-to-xls.bat
However this stops dead after getftp.bat has run.
What am I doing wrong? It's important that these commands all run sequentially.
Use call:
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
batch-parameters Specifies any command-line information required by the
batch program.
If you invoke other batch files without call then control is passed to them but not back again (which is what call changes).
use start command to launch it in a new window.
start /wait getftp.bat
Try using "Goto :EOF" rather than "exit" at the end of the batch file that you're calling - in your case, the getftp.bat file...
That's what fixed mine - tested on Win10 enterprise.

Resources