簡體   English   中英

使用@echo off和> nul后,批處理文件仍會打印錯誤消息

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM