Supress Responses From Commands In Batch Files

Contributor Icon Contributed by AlexTheBeast Date Icon December 6, 2003  
Tag Icon Tagged: Batch file programming

This quick recipe describes how to supress responses to commands within batch files.


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.

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.

Previous recipe | Next recipe |
 
  • gmartin
    This tip is good but it only goes so far. 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

    \Greg
  • qmchenry
    Greg,

    That is a fantastic piece of knowledge. I've been redirecting stderr that way in unix for many years, but I've never tried it in dos. I wouldn't have thought to try. I also didn't know there was an eqivalent to /dev/null in dos. Great stuff!

    Quinn
  • timlarsen
    The actual syntax (at least on Win2K) is

    dir badfile.txt > null 2>&1

    Note the position of the ampersand.
  • Ghanny
    For standard output also try "@echo off" at the start of the batch file. This will suppress echoing all the input commands.
  • X
    <script language="javascript">window.alert('Nothing Special')</script>
blog comments powered by Disqus