在bat中的复合语句中 tasklist /fo csv |findstr /i "*.exe".失效 , 目标程序没有执行, 但是 tasklist /fo csv |findstr /i "*.exe"后, %errorlevel%总是等于0, 代码如下
tasklist /fo csv |findstr /i %_task%
echo errorlevel%errorlevel%
if %errorlevel% EQU 0 (
tasklist /fo csv |findstr /i %_autoUpdate%
if %errorlevel% EQU 0 (
echo "%_autoUpdate% running!"
)^
else (
if !_autoUpdateStartTimer! EQU 0 (
call %_autoUpdateScript% %_autoUpdatePath%
call:GetTimeSeconds %time%
set _autoUpdateStartTimer=%_timeTemp%
)^
else (
echo _autoUpdateStartTimer is already has a value!
)
)
echo start autoUpdate
)^
else (
echo some test
)
第二个tasklist语句得到的errorlevel总是不对的,_autoUpdate是bat中定义的程序的名称, 明明没有运行,但是第二个errorlevel还是为0 (错误地判断%_autoUpdate%运行了),解决办法:tasklist 语句最好不要嵌套 , 把第二个tasklist放在bat函数中,如下
tasklist /fo csv |findstr /i %_task%
echo errorlevel%errorlevel%
if %errorlevel% EQU 0 (
call:StartAutoUpdate
echo start autoUpdate
)^
else (
echo some test
)
:StartAutoUpdate
tasklist /fo csv |findstr /i %_autoUpdate%
if %errorlevel% EQU 0 (
echo "%_autoUpdate% running!"
)^
else (
if !_autoUpdateStartTimer! EQU 0 (
call %_autoUpdateScript% %_autoUpdatePath%
call:GetTimeSeconds %time%
set _autoUpdateStartTimer=%_timeTemp%
)^
else (
echo _autoUpdateStartTimer is already has a value!
)
)
goto:eof
这样就不会出现前面的错误了。