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.
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).
To suppress output, use redirection to
NUL.There are two kinds of output that console commands use:
standard output, or
stdout,standard error, or
stderr.Of the two,
stdoutis used more often, both by internal commands, likecopy, and by console utilities, or external commands, likefindand others, as well as by third-party console programs.>NULsuppresses the standard output and works fine e.g. for suppressing the1 file(s) copied.message of thecopycommand. An alternative syntax is1>NUL. So,or
or
or
suppresses all of
COPY‘s standard output.To suppress error messages, which are typically printed to
stderr, use2>NULinstead. So, to suppress aFile Not Foundmessage thatDELprints when, well, the specified file is not found, just add2>NULeither at the beginning or at the end of the command line:or
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: