批处理文件中的任务列表的退出状态? [英] Exit status of tasklist in batch file?
问题描述
我在一个批处理文件的标签里执行下面的命令:
tasklist.exe / FIUSERNAME eq%USERDOMAIN%\%USERNAME%/ FIIMAGENAME eq%1/ FIPID eq %22> nul&& echo errorl:%errorlevel%
%1正在进程运行,%2是它的PID。
即使进程和它的PID匹配或不匹配,我得到errorl:1在o / p。
我不知道这里有什么错。任何想法?
在我看来,你不能使用errorlevel,
因为<即使没有找到pid,总是返回0。
我想,你必须解析tasklist的输出。
@echo off
setlocal enableDelayedExpansion
setcmd = tasklist.exe / FIUSERNAME eq %USERDOMAIN%\ USERNAME%/ FIIMAGENAME eq%1/ FIPID eq%2
for / Fdelims = *%% p in('!cmd !^ | findstr%2')do(
echo found %% p
)
I am executing following command in a label inside a batch file: tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" 2>nul && echo errorl:%errorlevel%
%1 is process running and %2 is its PID. Even if process and its PID matches or doesnt matches, I m getting "errorl:1" in o/p.
I am not sure whats wrong here. Any idea?
In my opinion, you can't use errorlevel at all,
because tasklist always returns a 0 even if the pid isn't found.
I suppose, you have to parse the output of tasklist.
@echo off
setlocal enableDelayedExpansion
set "cmd=tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2""
for /F "delims=*" %%p in ('!cmd! ^| findstr "%2" ') do (
echo found %%p
)
这篇关于批处理文件中的任务列表的退出状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!