如何使 Windows 批处理脚本完全静音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14686330/
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
How do I make a Windows batch script completely silent?
提问by Mike Sadler
There has been variants of this question asked for generations, but despite writing some quite complicated Windows scripts, I can't seem to find out how to make them actually silent.
这个问题的变体已经问了几代人,但尽管编写了一些非常复杂的 Windows 脚本,我似乎无法找到如何让它们真正静音。
The following is an excerpt from one of my current scripts:
以下是我当前脚本之一的摘录:
@ECHO OFF
SET scriptDirectory=%~dp0
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat
FOR /F %%f IN ('dir /B "%scriptDirectory%*.noext"') DO (
del "%scriptDirectory%%%f"
)
ECHO
The result of this is:
这样做的结果是:
C:\Temp> test.bat
1 file(s) copied.
File Not Found
Echo is off.
C:\Temp>
Whereas the "1 file(s) copied." is just annoying, the "File Not Found" makes the user think that something has gone wrong (which it hasn't - no files is fine).
而“复制了 1 个文件”。只是烦人,“找不到文件”让用户认为出现问题(它没有 - 没有文件很好)。
回答by Andriy M
To suppress output, use redirection to NUL
.
要抑制输出,请使用重定向到NUL
.
There are two kinds of output that console commands use:
控制台命令使用两种输出:
standard output, or
stdout
,standard error, or
stderr
.
标准输出,或者
stdout
,标准错误,或
stderr
。
Of the two, stdout
is used more often, both by internal commands, like copy
, and by console utilities, or externalcommands, like find
and others, as well as by third-party console programs.
在这两者中,stdout
更常用的是内部命令,如copy
和控制台实用程序,或外部命令,如find
和其他,以及第三方控制台程序。
>NUL
suppresses the standard output and works fine e.g. for suppressing the 1 file(s) copied.
message of the copy
command. An alternative syntax is 1>NUL
. So,
>NUL
抑制标准输出并正常工作,例如抑制命令的1 file(s) copied.
消息copy
。另一种语法是1>NUL
. 所以,
COPY file1 file2 >NUL
or
或者
COPY file1 file2 1>NUL
or
或者
>NUL COPY file1 file2
or
或者
1>NUL COPY file1 file2
suppresses all of COPY
's standard output.
抑制所有COPY
的标准输出。
To suppress error messages, which are typically printed to stderr
, use 2>NUL
instead. So, to suppress a File Not Found
message that DEL
prints when, well, the specified file is not found, just add 2>NUL
either at the beginning or at the end of the command line:
要抑制错误消息(通常打印到 )stderr
,请2>NUL
改用。因此,要抑制在未找到指定文件时打印的File Not Found
消息DEL
,只需2>NUL
在命令行的开头或结尾添加:
DEL file 2>NUL
or
或者
2>NUL DEL file
Although sometimes it may be a better idea to actually verify whether the file exists before trying to delete it, like you are doing in your own solution. Note, however, that you don't need to delete the files one by one, using a loop. You can use a single command to delete the lot:
尽管有时在尝试删除文件之前实际验证文件是否存在可能是一个更好的主意,就像您在自己的解决方案中所做的那样。但是请注意,您不需要使用循环逐个删除文件。您可以使用单个命令删除批次:
IF EXIST "%scriptDirectory%*.noext" DEL "%scriptDirectory%*.noext"
回答by Aacini
If you want that all normal output of your Batch script be silent (like in your example), the easiest way to do that is to run the Batch file with a redirection:
如果您希望 Batch 脚本的所有正常输出都是静默的(就像在您的示例中一样),最简单的方法是通过重定向运行 Batch 文件:
C:\Temp> test.bat >nul
This method does not require to modify a single line in the script and it still show error messages in the screen. To supress allthe output, including error messages:
此方法不需要修改脚本中的任何一行,并且仍会在屏幕上显示错误消息。要抑制所有输出,包括错误消息:
C:\Temp> test.bat >nul 2>&1
If your script have lines that produce output you want to appear in screen, perhaps will be simpler to add redirection to thoselineas instead of all the lines you want to keep silent:
如果您的脚本具有产生您希望出现在屏幕中的输出的行,那么将重定向添加到这些行而不是您想要保持沉默的所有行可能会更简单:
@ECHO OFF
SET scriptDirectory=%~dp0
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat
FOR /F %%f IN ('dir /B "%scriptDirectory%*.noext"') DO (
del "%scriptDirectory%%%f"
)
ECHO
REM Next line DO appear in the screen
ECHO Script completed >con
Antonio
安东尼奥
回答by Unknown
Just add a >NUL
at the end of the lines producing the messages.
只需>NUL
在生成消息的行的末尾添加一个。
For example,
例如,
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >NUL
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >NUL
回答by Bali C
You can redirect stdout to nul
to hide it.
您可以将 stdout 重定向到nul
以隐藏它。
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >nul
Just add >nul
to the commands you want to hide the output from.
只需添加>nul
到要隐藏输出的命令即可。
Hereyou can see all the different ways of redirecting the std streams.
在这里您可以看到重定向 std 流的所有不同方式。
回答by Hamzeen Hameem
Copies a directory named html& all its contents to a destination directory in silent mode. If the destination directory is not present it will still create it.
以静默模式将名为html的目录及其所有内容复制到目标目录。如果目标目录不存在,它仍将创建它。
@echo off
TITLE Copy Folder with Contents
set SOURCE=C:\labs
set DESTINATION=C:\Users\MyUser\Desktop\html
xcopy %SOURCE%\html\* %DESTINATION%\* /s /e /i /Y >NUL
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/I If destination does not exist and copying more than one file, assumes that destination must be a directory.
- /Y Suppresses prompting to confirm you want to overwrite an existing destination file.
/S 复制目录和子目录,除了空目录。
/E 复制目录和子目录,包括空目录。与 /S /E 相同。可用于修改/T。
/I 如果目标不存在并且复制多个文件,则假定目标必须是一个目录。
- /Y 禁止确认您要覆盖现有目标文件的提示。