Reputation: 43
I've got a batch file that executes a program along with sequential (numbered) macros and calls another batch file that monitors when it is finished before it begins the next iteration. I can't use Start /wait or other "ordering" commands when launching the program because it is started with a batch file and syntax that I can't avoid having to use. Once this loop is finished (after the program has opened and closed ~120 times for the typical usage pattern), I need to open it once more and run a different, non-sequential macro, but it fails once the loop has completed.
for /l %%x in (1,1,120) do (
echo %%x
"C:\File Path\ProgramStart.bat" -macro "C:\File Path\MyScript%%x.txt"
timeout 3
start /wait OpenChecker.bat )
"C:\File Path\ProgramStart.bat" -macro "C:\File Path\AnotherScript.txt"
For the sake of completeness, here is OpenChecker.bat:
:check
tasklist /FI "IMAGENAME eq Program.exe" 2>NUL | find /I /N "Program.exe">NUL
if "%ERRORLEVEL%" NEQ "0" exit
TIMEOUT 5
goto check
Despite all of the variations I've tried (both in finding a method that works for the extra-finicky program and changes in syntax in each batch file), I can't seem to get the instance of Program running AnotherScript (or anything else for that matter) to run. Thanks.
Upvotes: 4
Views: 4446
Reputation: 21
you can do this
cmd /k "for /l %%x in (1,1,120) do (echo %%x & call "C:\FilePath\ProgramStart.bat" -macro "C:\File Path\MyScript%%x.txt & timeout 3" & call OpenChecker.bat)"
call "C:\File Path\ProgramStart.bat" -macro "C:\File Path\AnotherScript.txt"
Upvotes: 0
Reputation: 70923
In general, when a batch file invokes another one, the flow execution is transfered to the called batch and does not return to the caller. To allow the caller retrieve the execution flow, it is necessary to use the call
command.
for /l %%x in (1,1,120) do (
echo %%x
call "C:\File Path\ProgramStart.bat" -macro "C:\File Path\MyScript%%x.txt"
timeout 3
call OpenChecker.bat
)
call "C:\File Path\ProgramStart.bat" -macro "C:\File Path\AnotherScript.txt"
NOTE: This solves the problem, but there is still a question. If the flow is transfered and it does not return to the caller, why does the loop iterate 120 times with the original code?
The for
loop has been parsed and is in execution. What happens is that when ProgramStart.bat
is invoked a new batch "context" is generated and it replaces the caller "context", but the for
command is still in memory in execution. It was parsed and started to execute as only one command. When ProgramStart.bat
ends, the "context" is discarded, but the for
command in memory is still in execution, but now it is not inside a batch "context" (both the original and the called "contexts" were discarded), but in a command line context.
Upvotes: 5
Reputation: 49086
Do you have ever read something about command call?
No, open a command prompt window, type call /?
, hit key RETURN and read help output now into the console window for that command.
See also my answer on How to call a batch file in the parent folder of current batch file? explaining the possibilities to run another batch file or a console or GUI application from within a batch file.
I suggest to use:
for /l %%x in (1,1,120) do (
echo %%x
call "C:\File Path\ProgramStart.bat" -macro "C:\File Path\MyScript%%x.txt"
timeout 3
call OpenChecker.bat
)
call "C:\File Path\ProgramStart.bat" -macro "C:\File Path\AnotherScript.txt"
Upvotes: -1