防止批处理中的命令输出 [英] Preventing output from commands in Batch

查看:179
本文介绍了防止批处理中的命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止CMD窗口中的输出?我知道该怎么做

How do you prevent output in the CMD window? I know how to do it for these

@回声关闭

超时/t 3/nobreak>空

timeout /t 3 /nobreak >nul

MKDIR文件2> nul

MKDIR Files 2>nul

但是我想知道所有命令的通用方式是什么,或者如何以不必不必询问的方式找出每个命令.我试图让XCopy静默复制,这样屏幕就不会出现垃圾邮件,但是我感觉我也需要其他命令来使用它.

But I was wondering what the general way is for all commands, or how to find out for each command that way I don't have to keep asking. I'm trying to get XCopy to copy silently, that way the screen doesn't get spammed, but I have a feeling I'll need it for other commands too.

推荐答案

有2个输出流: stdout (标准输出控制台)和 stderr (标准错误控制台)

There are 2 output streams: stdout (standard output console) and stderr (standard error console).

使用>nul或更正确的1>nul,写入 stdout 的文本将重定向到空设备.

With >nul or more correct 1>nul the text written to stdout is redirected to the null device.

使用2>nul,写入 stderr 的文本将重定向到空设备.

With 2>nul the text written to stderr is redirected to the null device.

>nul2>nul可以在任何命令上使用,也可以同时使用,并且应始终位于行尾.在某些特殊情况下,也可以在行的开头使用它们,但是大多数情况下,重定向操作符写在行的末尾.

>nul and 2>nul can be used on any command and also both at the same time and should be always at end of the line. They can be also used at beginning of a line which makes sense in some special cases, but most often the redirecting operators are written at end of a line.

使用选项/?(Windows)或-?(从Unix移植时)运行时,大多数控制台命令和应用程序都会显示帮助.打开命令提示符窗口,然后尝试使用xcopy /?

Most console commands and applications print a help when running with option /? (Windows) or -? (when ported from Unix). Open a command prompt window and try that with xcopy /?

xcopy 之类的某些命令具有特殊的选项,可禁止输出到 stdout . xcopy 的选项是/Q,它是安静的缩写.使用此选项会使xcopy也更快一点,因为将输出打印到 stdout 需要一些时间,如果将输出重定向到null设备,则当然浪费了时间.

Some commands like xcopy have special options to suppress output to stdout. The option for xcopy is /Q which is short for quiet. Using this option makes xcopy also faster a little bit as printing the output to stdout needs some time and is of course wasted time if the output is redirected to null device.

但是当将所有输出重定向到空设备时,请注意可能的错误停止.例如,在使用 xcopy 时,应另外使用选项/C/R,以避免由于错误而停止复制过程,或者避免在目标目录中显示只读文件而没有用户信息谁开始了复制过程,为什么由于使用/Q和其他>nul 2>nul而导致批处理作业无法完成.

But take care of possible halt on errors when redirecting all output to null device. For example on using xcopy the options /C and /R should be used additionally to avoid a halt of the copy process on error or a read-only file in target directory with no information of the user who started the copy process why batch job does not finish because of using /Q and additional >nul 2>nul.

在批处理文件中,始终应使用>nul,而从不使用1>nul,尽管通常也可以这样做.命令行解释器在执行批处理文件时会自动将每个>nul替换为 1>nul(空格+ 1>nul).在文件中将某些内容 echo 时应考虑到这一点,以避免重定向到文件中的每一行上经常出现不必要的尾随空格.

In batch files always >nul should be used and never 1>nul although that is often also possible. The command line interpreter replaces every >nul on execution of the batch file automatically by  1>nul (space + 1>nul). This should be taken into account when echo something into a file to avoid an often unwanted trailing space on each line redirected into a file.

有关更多信息,请参见Microsoft文章使用命令重定向运算符.

See Microsoft article Using command redirection operators for even more information.

这篇关于防止批处理中的命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆