如何批量处理WMIC命令的“未找到实例”?


1

我想批量终止一个进程的所有实例。我尝试使用:

@echo off
setlocal EnableDelayedExpansion
for /f "usebackq skip=1" %%r in (`wmic process where Name^="CALC.exe"  get Processid ^| findstr /r /v "^$"`) do SET procid=%%~r
IF [!procid!] NEQ [] (
  wmic process where Name="CALC.exe" call terminate >> NUL
) ELSE (
  GOTO :break
)
:break
SET procid=
endlocal

但是,如果不存在calc.exe实例,则我不想显示“没有可用实例”。另外,我希望每个calc.exe实例的显示不向下滚动一行

怎么做 ??

Answers:


0

我不想显示“无可用实例”。

for /f "usebackq skip=1" %%r in (`wmic process where Name^="CALC.exe" get Processid ^| findstr /r /v "^$"`) do SET procid=%%~r

您可以使用重定向运算符丢弃错误 2> nul

重定向到NUL(隐藏错误)

command 2> nul

笔记:

  • >必须使用转义^
  • null设备是一个特殊文件,该文件会丢弃所有写入其中的数据,但会报告写操作成功。

该命令for变为:

`wmic process where Name^="CALC.exe" get Processid 2^> nul ^| findstr /r /v "^$"`

另外,我希望每个calc.exe实例的显示不向下滚动一行

 wmic process where Name="CALC.exe" call terminate >> NUL

您可以使用重定向运算符丢弃多余的空白行 > NUL 2>&1

“终止”命令变为:

wmic process where Name="CALC.exe" call terminate >NUL 2>&1

全部放在一起

修改后的批处理文件:

@echo off
setlocal EnableDelayedExpansion
for /f "usebackq skip=1" %%r in (`wmic process where Name^="CALC.exe" get Processid 2^> nul ^| findstr /r /v "^$"`) do SET procid=%%~r
IF [!procid!] NEQ [] (
  wmic process where Name="CALC.exe" call terminate >NUL 2>&1
) ELSE (
  GOTO :break
)
:break
SET procid=
endlocal

进一步阅读

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.