[英]Batch file still prints error messages after using @echo off and >nul
我编写了一个批处理文件,以在计算机尝试关闭之前使用taskkill
函数结束特定程序来关闭所有程序。 我在开始时使用@echo off
,并在taskkill函数的末尾写了>null
,但是我仍然得到一个命令提示符窗口,其中详细列出了taskkill找不到被告知关闭的程序的错误。 我正在运行Windows 10,如果有帮助。
这是行为不正确的两行代码:
@echo off
taskkill -f -t -im chrome.exe -im skype.exe -im WINWORD.exe >nul
这是完整的批处理文件,如果您想查看的话:
REM Closes all programs, THEN turns off computer
@echo off
:: If you want to make a command prompt window appear, change this to “@echo on”
taskkill -f -t -im chrome.exe -im skype.exe -im WINWORD.exe >nul
:: Add all your program names here and an “/im” before them. Also if you want them to give you prompts before
:: shutting down, delete the "-f". Delete the ">nul"s if you want error messages
timeout 4 >nul
:: This will make the computer wait for between 3 and 4 seconds. This is designed for slower computers,
:: you can make the time delay less if you want
shutdown.exe -p -f >nul
:: This shuts down computer. -p makes it not have a time delay or display a message, -f quits programs just in case
exit
:: Exits in case it prevents computer from shutting down
谢谢
StdOut和StdError不同。 您只重定向了StdOut。
要将StdError重定向到StdOut所要去的任何地方,请放在行上的某个位置
2>&1
Cmd实际上更改为>
到1>
(如果删除echo off
则会在批处理中看到此内容)。
将StdError重定向到其他地方
2> filename.txt
0=keyboard
1=screen for normal output
2=screen for error
3-9=whatever other files Cmd has open
> nul丢弃发送到stdout或文件描述符1的输出。大多数命令将错误消息发送到单独的通道stderr(2)。 您可以使用2> nul
将stderr重定向到2> nul
。 (确保2
和>
之间没有空格;否则, 2
将作为命令的参数。)要同时丢弃stderr和stdout,通常可以这样做:
taskkill ... >nul 2>&1
这意味着将stdout重定向到nul,也将stderr与stdout一样对待。
这样尝试:
@echo off
set process=mshta.exe,chrome.exe,calc.exe,skype.exe,iexplore.exe
For %%a in (%process%) Do Call :KillMyProcess %%a
:KillMyProcess
Taskkill /IM "%1" /F >nul 2>&1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.