我想编写一个批处理文件,检查进程是否正在运行,如果是,则执行一个操作,如果不是则执行另一个操作。
我知道我可以使用任务列表列出所有正在运行的进程,但有没有更直接检查特定进程的简单方法?
看起来这应该有效,但事实并非如此:
tasklist /fi "imagename eq firefox.exe" /hn | MyTask
IF %MyTask%=="" GOTO DO_NOTHING
'do something here
:DO_NOTHING
使用atzz提供的解决方案,这是一个完整的工作演示:
编辑:简化,并修改为在WinXP和Vista下工作
echo off
set process_1="firefox.exe"
set process_2="iexplore.exe"
set ignore_result=INFO:
for /f "usebackq" %%A in (`tasklist /nh /fi "imagename eq %process_1%"`) do if not %%A==%ignore_result% Exit
for /f "usebackq" %%B in (`tasklist /nh /fi "imagename eq %process_2%"`) do if not %%B==%ignore_result% Exit
start "C:\Program Files\Internet Explorer\iexplore.exe" www.google.com
答案 0 :(得分:6)
您可以使用“for / f”构造来分析程序输出。
set running=0
for /f "usebackq" %%T in (`tasklist /nh /fi "imagename eq firefox.exe"`) do set running=1
另外,坚持
是个好主意setlocal EnableExtensions
在脚本开始时,以防用户默认禁用它。
答案 1 :(得分:2)
一些选项:
http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/win2kcommands_0401.html#ps
http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/win2kcommands_0401.html#ps