windows 等待可执行文件完成运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5375609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for executable to finish running
提问by Pillblast
Can you run two executables in a batch file and wait for the first process to end before starting the next?
您可以在批处理文件中运行两个可执行文件并等待第一个进程结束后再开始下一个进程吗?
回答by nimizen
Use start /wait:
使用开始/等待:
:NOTEPAD
start /wait notepad.exe
IF %ERRORLEVEL% == 0 goto NEXTITEM1
else goto QUIT
:NEXTITEM1
start /wait mplayer.exe
IF %ERRORLEVEL% == 0 goto NEXTITEM2
else goto QUIT
:NEXTITEM2
start /wait explorer.exe
IF %ERRORLEVEL% == 0 goto NEXTITEM3
else goto QUIT
:NEXTITEM3
REM You get the idea...
:QUIT
exit
In addition, use NT CMD rather than BAT (myscript.cmd).
此外,使用 NT CMD 而不是 BAT (myscript.cmd)。
In response to the comments, brackets removed from the above script around %ERRORLEVEL%. The following seems to behave as expected:
作为对评论的回应,从上面的脚本中删除了 %ERRORLEVEL% 周围的括号。以下行为似乎符合预期:
:NOTEPAD
start /wait notepad.exe || goto QUIT
:NEXTITEM1
start /wait mplayer2.exe || goto QUIT
:NEXTITEM2
REM You get the idea...
:QUIT
exit
The statement after the double-pipe only executes if what is before it fails.
双管道之后的语句仅在它之前的语句失败时才执行。
回答by npocmaka
one shorter way:
一种更短的方法:
notepad.exe|more
even this can be used:
甚至可以使用:
notepad|rem
though eventually with more
you'll able to catch some output if there's any.And this is the reason it works - the piped command waits for input until the .exe is finished.
尽管最终more
您将能够捕获一些输出(如果有的话)。这就是它起作用的原因 - 管道命令等待输入,直到 .exe 完成。