Search code examples
windowscmdprompttext-files

How do I programatically capture the output of a command on Windows


What the question says.

Ultimately what I want is to execute gcc and capture the output if there's an error. The problem is errors are written to stderr instead of stdout. On Linux I can do

gcc foo.c 2>&1

How can I accomplish this on Windows?


Solution

  • There is. Simply right click into the console window, select Mark. With your mouse select the desired area and right click. Now you can paste it into a text file with Ctrl-V.

    If you need the output of a program into a text file, run it like this:

    myprogram.exe > myfile.txt

    See here about redirecting:
    1. Using command redirection operators
    2. Redirecting Error Messages from Command Prompt: STDERR/STDOUT

    You can do what you want like this: D:\>dir 1> test.txt 2> testerr.txt