Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 923
Doug Avatar asked Dec 13 '11 06:12

Doug


People also ask

How do I stop a batch file from exiting?

If you're creating a batch file and want the MS-DOS window to remain open, add PAUSE to the end of your batch file. This prompts the user to Press any key. Until the user presses any key, the window remains open instead of closing automatically.

How do I pause a batch file after execution?

Execution of a batch script can also be paused by pressing CTRL-S (or the Pause|Break key) on the keyboard, this also works for pausing a single command such as a long DIR /s listing. Pressing any key will resume the operation.


1 Answers

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 
like image 121
pmod Avatar answered Sep 17 '22 14:09

pmod