我正在运行一个简单的批处理脚本来启动 2 个程序。如何添加一行来检查程序是否正在运行。然后我怎样才能跳过程序的启动并进入下一行。
@echo off
tasklist /FI "IMAGENAME eq Test123.exe" 2>NUL | find /I /N "Test123.exe">NUL
if "%ERRORLEVEL%"=="0" echo program is running
cd "C:\Program Files (x86)\Test123"
start Test123.exe
cd "C:\Program Files (x86)\Test123"
start OtherTest123.exe
exit
您可以使用标签和 GOTO 来控制文件的流程,如下所示:
@echo off
tasklist /FI "IMAGENAME eq Test123.exe" 2>NUL | find /I /N "Test123.exe">NUL
if "%ERRORLEVEL%"=="0" GOTO nextprogram
cd "C:\Program Files (x86)\Test123"
start Test123.exe
:nextprogram
cd "C:\Program Files (x86)\Test123"
start OtherTest123.exe
exit
查看更多关于 goto 的详细说明:https://ss64.com/nt/goto.html
@user3154458 的答案是可行的,但为了可读性和维护性,我会推荐一个代码块。此外,使用
CD /D
将确保默认驱动器也同时更改。
@echo off
PUSHD "C:\Program Files (x86)\Test123"
tasklist /FI "IMAGENAME eq Test123.exe" 2>NUL | find /I /N "Test123.exe">NUL
if NOT "%ERRORLEVEL%"=="0" (start Test123.exe)
start OtherTest123.exe
POPD
exit /B %ERRORLEVEL%
我建议也许这样做:
@Echo Off
CD /D "%ProgramFiles(x86)%\Test123" 2>Nul||Exit /B
TaskList|FindStr/I "\<Test123.exe\>">Nul&&(Start "" "OtherTest123.exe")||Start "" "Test123.exe"