批处理文件等待具有复杂命令行完成的程序

时间:2017-08-29 08:38:26

标签: batch-file

我需要运行此命令

LEProc.exe -runas ba954abf-2cf7-4efc-90c3-4b6d90aed470 "%~dp0Release\noppack.exe" data script

完成后,应处理批次中的下一行。现在,有很多方法,但我更喜欢像START /WAIT那样简单的事情,但无论我怎么努力,用START运行此结果都会导致 The system cannot find the file specified.

在恶魔.devin的回答之后,这就是解决方案:

:CHECK_RUNNING

    set errorlevel=
    tasklist /fi "imagename eq noppack.exe" | find /i "noppack.exe"> NUL
    if /i %errorlevel% GTR 0 goto CONTINUE
    ping -n 1 -w 5000 1.1.1.1 > nul
    goto CHECK_RUNNING

:CONTINUE
[...]

2 个答案:

答案 0 :(得分:2)

看起来LEproc.exe会产生另一个进程 然后它返回并且当你想知道Noppack.exe何时完成时没有等待LEproc.exe的用途。

如果nopppack.exe仍在运行,你应该检查tasklist / pslist和一个循环(有一个延迟)。

答案 1 :(得分:1)

我没有测试过这个,但也许这可以帮助..

@echo off
goto begin

:: INFO: Check to see if a process is running and if so wait before launching
::       a new process. After checking for a process this .bat file will wait 
::       for a set amount of seconds declared be the user before checking and
::       looping again or moving onto the next command.

:: EDIT: Change "seconds=" to "seconds=15000" to set a time of 15 seconds for
::       the wait period. For 5 seconds change it to "seconds=5000" For extra
::       commands you should add "set app3=AnotherProcess.exe" and repeat for
::       more if need be but be sure to compensate for the new command below.

:begin

:: Set length in seconds here
set seconds=

:: Set your processes here
set app1=noppack.exe
set app2=NewProcess.exe

:: Do not edit


:: First loop check
echo.
echo.
echo Starting the LEProc.exe process...

LEProc.exe -runas ba954abf-2cf7-4efc-90c3-4b6d90aed470 "%~dp0Release\noppack.exe" data script

:check_one
    cls

    set errorlevel=

    tasklist /fi "imagename eq %app1%" | find /i "%app1%"> NUL

    if /i %errorlevel% GTR 0 goto complete_one

    echo.
    echo The %app1% process is running!
    echo.
    echo Will check again in a few seconds.
    echo.
    ping -n 1 -w %seconds% 1.1.1.1 > nul
    goto check_one

:: First process complete
:complete_one
    echo.
    echo The %app1% process has finished!
    echo.
    echo Starting the %app2% process...

    START /WAIT %app2%

:: Second loop check
:check_two
    cls

    set errorlevel=

    tasklist /fi "imagename eq %app2%" | find /i "%app2%"> NUL

    if /i %errorlevel% GTR 0 goto complete_two

    echo.
    echo The %app2% process is running!
    echo.
    echo Will check again in a few seconds.
    echo.
    ping -n 1 -w %seconds% 1.1.1.1 > nul
    goto check_two

:: Second process complete
:complete_two
    echo.
    echo The %app2% process has finished!

pause

阅读评论部分以了解用法和要编辑的内容。

如果@LotPings不正确,请将app1=noppack.exe更改为app1=LEProc.exe

希望这有帮助!

=)