4

How do you prevent output in the CMD window? I know how to do it for these

@echo off

timeout /t 3 /nobreak >nul

MKDIR Files 2>nul

But I was wondering what the general way is for all commands, or how to find out for each command that way I don't have to keep asking. I'm trying to get XCopy to copy silently, that way the screen doesn't get spammed, but I have a feeling I'll need it for other commands too.

user3717248
  • 41
  • 1
  • 2

2 Answers2

4

There are 2 output streams: stdout (standard output console) and stderr (standard error console).

With >nul or more correct 1>nul the text written to stdout is redirected to the null device.

With 2>nul the text written to stderr is redirected to the null device.

>nul and 2>nul can be used on any command and also both at the same time and should be always at end of the line. They can be also used at beginning of a line which makes sense in some special cases, but most often the redirecting operators are written at end of a line.

Most console commands and applications print a help when running with option /? (Windows) or -? (when ported from Unix). Open a command prompt window and try that with xcopy /?

Some commands like xcopy have special options to suppress output to stdout. The option for xcopy is /Q which is short for quiet. Using this option makes xcopy also faster a little bit as printing the output to stdout needs some time and is of course wasted time if the output is redirected to null device.

But take care of possible halt on errors when redirecting all output to null device. For example on using xcopy the options /C and /R should be used additionally to avoid a halt of the copy process on error or a read-only file in target directory with no information of the user who started the copy process why batch job does not finish because of using /Q and additional >nul 2>nul.

In batch files always >nul should be used and never 1>nul although that is often also possible. The command line interpreter replaces every >nul on execution of the batch file automatically by  1>nul (space + 1>nul). This should be taken into account when echo something into a file to avoid an often unwanted trailing space on each line redirected into a file.

See Microsoft article Using command redirection operators for even more information.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

The "general way" is exactly what you're doing, which is to redirect standard output to nul using >nul. It should always be the very last part of each line.

(The mkdir example you're using redirects standard error (2>nul instead of standard output; I'm not sure why it would be doing that, but presumably you know why that's being done.)

The problem with doing so, though, is that you don't get any information about what worked and what failed, which makes it difficult to track down problems. When you need to not show the information on screen but want to make it available later if needed, redirect to a file instead.

REM Redirect first command to log, truncating anything
REM that existed in the file before.
mkdir Files > temp.log

REM Copy some files into the folder just created, appending
REM the results to the same log file
xcopy *.txt Files\ >> temp.log
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • What is the difference between > and >>? – user3717248 Jun 07 '14 at 17:06
  • `>` truncates the existing file, and `>>` appends (adds to) to the file if it exists already. Both of them will create the file if it does not exist already, but `>>` will add text at the end of the existing content. – Ken White Jun 07 '14 at 17:12