How do you stop a Windows Batch file from exiting early?

Doug picture Doug · Dec 13, 2011 · Viewed 11.9k times · Source

I have a windows batch file that looks similar to:

C:\DoStuff.cmd
move output.bak C:\newfolder\output.bak

The problem i have is that DoStuff.cmd executes a java program that once complete exits the batch run back to the command prompt. Line 2 never gets hit.

i have tried the following instead to execute the command in a new window:

start "My program" /WAIT C:\DoStuff.cmd
move output.bak C:\newfolder\output.bak

What happens with the above is that the new command window spawns the cmd file runs and exits back to a waiting command prompt and the window never closes, leaving the first command window waiting and the second doing nothing stuck after finishing step one.

How do i execute the first command without it having control of the batch run somehow?

many thanks in advance

Answer

pmod picture pmod · Dec 13, 2011

You can use DOS call command:

@echo off
call C:\DoStuff.cmd
echo Exit Code = %ERRORLEVEL%

After getting error code you can proceed for example with:

if "%ERRORLEVEL%" == "1" exit /B 1