批处理程序以检查进程是否存在 [英] Batch program to to check if process exists

查看:23
本文介绍了批处理程序以检查进程是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个批处理程序,它会检查进程 notepad.exe 是否存在.

I want a batch program, which will check if the process notepad.exe exists.

如果 notepad.exe 存在,则结束进程,

if notepad.exe exists, it will end the process,

else 批处理程序将自行关闭.

else the batch program will close itself.

这是我所做的:

@echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit

但它不起作用.我的代码有什么问题?

But it doesn't work. What is the wrong in my code?

推荐答案

TASKLIST 未设置错误级别.

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit

应该完成这项工作,因为只有在未找到任务时,:"才会出现在 TASKLIST 输出中,因此 FIND 会将错误级别设置为 0 表示 not found1 表示 found

should do the job, since ":" should appear in TASKLIST output only if the task is NOT found, hence FIND will set the errorlevel to 0 for not found and 1 for found

尽管如此,

taskkill/f/im "notepad.exe"

taskkill /f /im "notepad.exe"

如果记事本任务存在,它将杀死一个记事本任务——如果记事本任务不存在,它就什么也不做,所以你真的不需要测试——除非你想做别的事情......也许就像

will kill a notepad task if it exists - it can do nothing if no notepad task exists, so you don't really need to test - unless there's something else you want to do...like perhaps

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"&exit

这似乎按照您的要求执行 - 如果存在,则终止记事本进程,然后退出 - 否则继续批处理

which would appear to do as you ask - kill the notepad process if it exists, then exit - otherwise continue with the batch

这篇关于批处理程序以检查进程是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆