874

I'm trying to redirect all output (standard output and standard error) of a Windows command to a single file:

C:\ cd \ dir 1> a.txt 2> a.txt 

Output:

The process cannot access the file because it is being used by another process.

Is it possible, or should I just redirect to two separate files?

3

7 Answers 7

1389

You want:

dir > a.txt 2>&1 

The syntax 2>&1 will redirect 2 (stderr) to 1 (stdout). You can also hide messages by redirecting to NUL. More explanation and examples are on the Microsoft documentation page Redirecting error messages from Command Prompt: STDERR/STDOUT.

Sign up to request clarification or add additional context in comments.

7 Comments

this is great for hiding all output.. net stop w3svc >NUL 2>&1.. thanks!
@wasatchwizard Ithink I had trouble with that, but >NUL 2>NUL worked fine
If there is a Handle, there cannot be a space between the Handle (i.e. 2) and the redirect operator (i.e. >). Therefore 2> 2.txt works (or 2> &1) 2 > 2.txt does not; 2 > &1 does not.
if you want to append, use >> instead of > i.e. dir >> a.txt 2>&1 it is very useful for system logs and exeptions.
Perhaps add some information as per DelboyJays answer's after note?
|
247

Anders Lindahl's answer is correct, but it should be noted that if you are redirecting stdout to a file and want to redirect stderr as well then you MUST ensure that 2>&1 is specified AFTER the 1> redirect, otherwise it will not work.

REM *** WARNING: THIS WILL NOT REDIRECT STDERR TO STDOUT **** dir 2>&1 > a.txt 

14 Comments

Is it explaned anywhere why putting 2>&1 before 1> will not achieved the intended effect? I strongly suspect this has got to do with the way "cmd" parses commands that gives two different meanings depending on the order in which you specify the redirection. But are the semantic rules documented anywhere because I reckon this is something worthwhile to learn as it could waste hours.
@igbgotiz 2>&1 means 'redirect stream 2 to stream 1'. So you need to set up stream 1 first
@FrinkTheBrave but stream 1 is standard output (e.g. console) if not explicitly specified. That still does not explain it imho.
@MDeSchaepmeester, if you do dir 2>&1 > a.txt, you're first redirecting (>) stream 2 (stderr) to stream 1 (stdout). Then, after both of them are already joined together, you're redirecting stdout (> with no specifier) to the file. If you want stderr to go somewhere else, you can't join it with stdout first.
The usual way to do that is to use tee which allows something to go to a file, and still go to stdout/stderr. If you redirect a stream, it doesn't still go to the original destination. It's been redirected.
|
105

Background info from Microsoft documentation

While the accepted answer to this question is correct, it really doesn't do much to explain why it works, and since the syntax is not immediately clear I did a quick www search to find out what was actually going on. In the hopes that this information is helpful to others, I'm posting it here.

Taken from the Microsoft documentation page:
Redirecting error messages from Command Prompt: STDERR/STDOUT

Summary

When redirecting output from an application using the > symbol, error messages still print to the screen. This is because error messages are often sent to the Standard Error stream instead of the Standard Out stream.

Output from a console (Command Prompt) application or command is often sent to two separate streams. The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol. This selects the second output stream that is STDERR.

Example

The command dir file.xxx (where file.xxx does not exist) will display the following output:

Volume in drive F is Candy Cane Volume Serial Number is 34EC-0876
File Not Found

If you redirect the output to the NUL device using dir file.xxx > nul, you will still see the error message:

File Not Found

To redirect the error message to NUL, use the following command:

dir file.xxx 2> nul 

Or, you can redirect the output to one place, and the errors to another.

dir file.xxx > output.msg 2> output.err 

You can print the errors and standard output to a single file by using the &1 command to redirect the output for STDERR to STDOUT and then sending the output from STDOUT to a file:

dir file.xxx 1> output.msg 2>&1 

Comments

47

To add the stdout and stderr to the general logfile of a script:

dir >> a.txt 2>&1 

1 Comment

The >> appends to the file where the > overwrites the file.
15

Correct, file handle 1 for the process is STDOUT, redirected by the 1> or by > (1 can be omitted, by convention, the command interpreter [cmd.exe] knows to handle that). File handle 2 is STDERR, redirected by 2>.

Note that if you're using these to make log files, then unless you're sending the outut to _uniquely_named_ (eg date-and-time-stamped) log files, then if you run the same process twice, the redirected will overwrite (replace) the previous log file.

The >> (for either STDOUT or STDERR) will APPEND not REPLACE the file. So you get a cumulative logfile, showwing the results from all runs of the process - typically more useful.

Happy trails...

Comments

7

There is, however, no guarantee that the output of SDTOUT and STDERR are interweaved line-by-line in timely order, using the POSIX redirect merge syntax.

If an application uses buffered output, it may happen that the text of one stream is inserted in the other at a buffer boundary, which may appear in the middle of a text line.

A dedicated console output logger (I.e. the "StdOut/StdErr Logger" by 'LoRd MuldeR') may be more reliable for such a task.

See: MuldeR's OpenSource Projects

1 Comment

Tested "StdOut/StdErr Logger" and it has a similar issue - stdout and stderr appears not in the same order as they would appear in the console.
5

In a batch file (Windows 7 and above), I found this method most reliable:

Call :logging >"C:\Temp\NAME_Your_Log_File.txt" 2>&1 :logging TITLE "Logging Commands" ECHO "Read this output in your log file" ECHO .. Prompt $_ COLOR 0F 

Obviously, use whatever commands you want and the output will be directed to the text file. Using this method is reliable. However, there isn't any output on the screen.

1 Comment

(basically the same answer given a few times years ago.) You can force output to screen with >con echo This goes to screen Also useful for user input >con set /p "var="Input: " Note: those lines will only appear on screen and not be redirected to the file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.