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 OFFSET scriptDirectory=%~dp0COPY %scriptDirectory%test.bat %scriptDirectory%test2.batFOR /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 FoundEcho 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文件”只是令人讨厌,而“文件未找到”则会让用户认为出了问题(它并没有——没有文件是好的)。
5 个解决方案
#1
55
To suppress output, use redirection to NUL
.
要抑制输出,请使用重定向到NUL。
There are two kinds of output that console commands use:
控制台命令使用两种输出:
-
standard output, or
stdout
,标准输出或stdout,
-
standard error, or
stderr
.标准错误,或者stderr。
Of the two, stdout
is used more often, both by internal commands, like copy
, and by console utilities, or external commands, 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个文件。复制命令的消息。另一种语法是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.
抑制所有拷贝的标准输出。
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。因此,为了抑制未找到的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"
#2
8
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:
如果您希望批处理脚本的所有正常输出都是静默的(如您的示例),最简单的方法是运行批处理文件并进行重定向:
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 all the 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 those lineas instead of all the lines you want to keep silent:
如果您的脚本中有生成您希望在屏幕中显示的输出的行,那么在这些行中添加重定向可能会比您希望保持静默的所有行更简单:
@ECHO OFFSET scriptDirectory=%~dp0COPY %scriptDirectory%test.bat %scriptDirectory%test2.batFOR /F %%f IN ('dir /B "%scriptDirectory%*.noext"') DO (del "%scriptDirectory%%%f")ECHOREM Next line DO appear in the screenECHO Script completed >con
Antonio
安东尼奥
#3
3
Just add a >NUL
at the end of the lines producing the messages.
在生成消息的行末尾添加一个>NUL。
For example,
例如,
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >NUL
复制% scriptDirectory %测试。蝙蝠% scriptDirectory % test2。蝙蝠>空
#4
2
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添加到要隐藏输出的命令中。
Here you can see all the different ways of redirecting the std streams.
在这里,您可以看到所有重定向std流的不同方法。
#5
1
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 offTITLE Copy Folder with Contentsset SOURCE=C:\labsset DESTINATION=C:\Users\MyUser\Desktop\htmlxcopy %SOURCE%\html\* %DESTINATION%\* /s /e /i /Y >NUL
-
/S Copies directories and subdirectories except empty ones.
/S复制目录和子目录(空目录除外)。
-
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/E复制目录和子目录,包括空目录。一样/ S / E。可用于修改/T。
-
/I If destination does not exist and copying more than onefile, assumes that destination must be a directory.
如果目的地不存在,并且复制多个文件,假设目的地必须是一个目录。
- /Y Suppresses prompting to confirm you want to overwritean existing destination file.
- /Y抑制提示以确认您想要覆盖现有目标文件。
#1
55
To suppress output, use redirection to NUL
.
要抑制输出,请使用重定向到NUL。
There are two kinds of output that console commands use:
控制台命令使用两种输出:
-
standard output, or
stdout
,标准输出或stdout,
-
standard error, or
stderr
.标准错误,或者stderr。
Of the two, stdout
is used more often, both by internal commands, like copy
, and by console utilities, or external commands, 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个文件。复制命令的消息。另一种语法是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.
抑制所有拷贝的标准输出。
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。因此,为了抑制未找到的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"
#2
8
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:
如果您希望批处理脚本的所有正常输出都是静默的(如您的示例),最简单的方法是运行批处理文件并进行重定向:
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 all the 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 those lineas instead of all the lines you want to keep silent:
如果您的脚本中有生成您希望在屏幕中显示的输出的行,那么在这些行中添加重定向可能会比您希望保持静默的所有行更简单:
@ECHO OFFSET scriptDirectory=%~dp0COPY %scriptDirectory%test.bat %scriptDirectory%test2.batFOR /F %%f IN ('dir /B "%scriptDirectory%*.noext"') DO (del "%scriptDirectory%%%f")ECHOREM Next line DO appear in the screenECHO Script completed >con
Antonio
安东尼奥
#3
3
Just add a >NUL
at the end of the lines producing the messages.
在生成消息的行末尾添加一个>NUL。
For example,
例如,
COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >NUL
复制% scriptDirectory %测试。蝙蝠% scriptDirectory % test2。蝙蝠>空
#4
2
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添加到要隐藏输出的命令中。
Here you can see all the different ways of redirecting the std streams.
在这里,您可以看到所有重定向std流的不同方法。
#5
1
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 offTITLE Copy Folder with Contentsset SOURCE=C:\labsset DESTINATION=C:\Users\MyUser\Desktop\htmlxcopy %SOURCE%\html\* %DESTINATION%\* /s /e /i /Y >NUL
-
/S Copies directories and subdirectories except empty ones.
/S复制目录和子目录(空目录除外)。
-
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/E复制目录和子目录,包括空目录。一样/ S / E。可用于修改/T。
-
/I If destination does not exist and copying more than onefile, assumes that destination must be a directory.
如果目的地不存在,并且复制多个文件,假设目的地必须是一个目录。
- /Y Suppresses prompting to confirm you want to overwritean existing destination file.
- /Y抑制提示以确认您想要覆盖现有目标文件。