Search code examples
windowscommand-linecmdpipe

How can I redirect Windows cmd standard output and standard error to a single file?


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?


Solution

  • 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.