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

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

问题描述

我想一个批处理程序,该程序将检查过程的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 the batch program will close itself.

下面是我干了什么:

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

但它不工作。错在我的code是什么?

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 输出仅如果任务没找到,因此查找将设置ERRORLEVEL为 0 未找到 1 发现

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天全站免登陆