抑制 Windows 命令行中的错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20298682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suppress error messages in Windows commandline
提问by charqus
Let's say I already have a folder created on the next path file: "C:\users\charqus\desktop\MyFolder"
, and I run the next command on CMD:
假设我已经在下一个路径 file: 上创建了一个文件夹"C:\users\charqus\desktop\MyFolder"
,然后在 CMD 上运行下一个命令:
mkdir "C:\users\charqus\desktop\MyFolder"
I get a message like this: "A subdirectory or file C:\users\charqus\desktop\MyFolder already exists".
我收到这样的消息:“子目录或文件 C:\users\charqus\desktop\MyFolder 已经存在”。
Therefore, is there any command in the commandline to get rid of this returned messages? I tried echo off but this is not what I looking for.
因此,命令行中是否有任何命令可以删除此返回的消息?我试过回声,但这不是我想要的。
回答by Roger Rowland
Redirect the output to nul
将输出重定向到 nul
mkdir "C:\users\charqus\desktop\MyFolder" > nul
Depending on the command, you may also need to redirect errors too:
根据命令,您可能还需要重定向错误:
mkdir "C:\users\charqus\desktop\MyFolder" > nul 2> nul
Microsoft describes the options here, which is useful reading.
微软在这里描述了这些选项,这是有用的阅读。
回答by user1976
A previous answer shows how to squelch all the output from the command. This removes the helpful error text that is displayed if the command fails. A better way is shown in the following example:
先前的答案显示了如何抑制命令的所有输出。这将删除在命令失败时显示的有用错误文本。以下示例显示了更好的方法:
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 4E99-B781
Directory of C:\test
20/08/2015 20:18 <DIR> .
20/08/2015 20:18 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 214,655,188,992 bytes free
C:\test>dir new_dir >nul 2>nul || mkdir new_dir >nul 2>nul || mkdir new_dir
C:\test>dir new_dir >nul 2>nul || mkdir new_dir >nul 2>nul || mkdir new_dir
As is demonstrated above this command successfully suppress the original warning. However, if the directory can not be created, as in the following example:
如上所示,此命令成功抑制了原始警告。但是,如果无法创建目录,如下例所示:
C:\test>icacls c:\test /deny "Authenticated Users":(GA)
processed file: c:\test
Successfully processed 1 files; Failed processing 0 files
C:\test>dir new_dir2 >nul 2>nul || mkdir new_dir2 >nul 2>nul || mkdir new_dir2
Access is denied.
Then as can be seen, an error message is displayed describing the problem.
然后可以看到,会显示描述问题的错误消息。