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

蓝咒 提交于 2019-12-10 00:38:14

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/8485081/how-do-you-stop-a-windows-batch-file-from-exiting-early

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!