I'm trying to redirect all output (stdout + stderr) of a Windows command to a single file:
C:\>dir 1> a.txt 2> a.txt
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?
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.
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
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.
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 1> output.msg 2>&1 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
To add the stdout and stderr to the general logfile of a script:
dir >> a.txt 2>&1
>>
appends to the file where the >
overwrites the file.
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...
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
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 is NO output on the screen.
>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.
Success story sharing
net stop w3svc >NUL 2>&1
.. thanks!2> 2.txt
works (or2> &1
)2 > 2.txt
does not;2 > &1
does not.