How to Supress Prompts & Responses from commands in Batch Files
If you're new here, you may want to subscribe to Windows Reference RSS feed Thanks for visiting!
Commands such as deleting all files can complicate batch files because the system prompts for confirmation.
For example check the following command
del *.* replies with *.*, Are you sure (Y/N)?
Now if you don’t want to display prompt By using echo this will automatically reply for you
echo y | del *.*
or
del /q *.*
To supress the “File Not Found” error when trying to delete files from an empty directory, use this code instead:
if exist *.* echo y| del *.*
or
if exist *.* | del /q *.*
During the execution of your batch files, users can see your commands on the screen. Most of the time this is good for debugging purposes; however, often it’s better to run your batch file silently.
Solution 1
Any command that is followed by “> nul” will not be shown on the screen. The greater sign directs the output to null (nowhere).
For example, copy *.* *.bak will show you as it copies all the files in the directory and renames them with the bak extension.
copy *.* *.bak > nul will silently complete its work without placing anything on the screen.
Solution 2
It will redirect STDOUT to null. Errors will still be displayed. As an example, run dir badfile.txt >nul and you will still see “File not found” displayed. That’s because errors are written to STDERR
To redirect STDERR to nul as well, try this:
dir badfile.txt > nul 2>&1
Of course you may want to see the errors. So a better tack to take could be to write to a log file
dir badfile.txt >log.txt 2>&1
Random Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically each day to your feed reader.






No comments yet.
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>