- Redirect Output from the Windows Command Line to a Text File
- How Windows Command Prompt Output Works
- Redirect Standard Output Write to New File
- Redirect Standard Output Writes to the Same File
- Redirect Standard Error To a File
- Redirect All Output Writes to a Same File
- Silencing Standard or Error Output Streams
- How to Redirect Command Output to a File
- Use redirection operators to save a command’s results to a file
- How to Use Redirection Operators
- The Append Redirection Operator
- Use Redirection Operators in Batch Files
- Redirect Windows cmd stdout and stderr to a single file
- 7 Answers 7
- Background info from MSKB
- From MSKB110930
- Redirecting Error Messages from Command Prompt: STDERR/STDOUT
- Summary
- Example
- How do you redirect standard input to a file in the Windows command line?
- 4 Answers 4
Redirect Output from the Windows Command Line to a Text File
Avoid scrolling and/or save the generated output
One of the most useful ways to log and troubleshoot the behavior of commands or batch jobs that you run on Windows is to redirect output to a file.
However, there are a few different ways you can redirect command line writes to a file. The option you choose depends on how you want to view your command output.
How Windows Command Prompt Output Works
When you type a command in the Windows console (command prompt), the output from that command goes to two separate streams.
- STDOUT: Standard Out is where any standard responses from commands go. For example the standard response for the DIR command is a list of files inside a directory.
- STDERR: Standard Error is where any error messages go if there’s a problem with the command. For example if there aren’t any files in the directory, the DIR command will output “File Not Found” to the Standard Error stream.
You can redirect output to a file in Windows for both of these output streams.
Redirect Standard Output Write to New File
There are two ways you can redirect standard output of a command to a file. The first is to send the command output write to a new file every time you run the command.
To do this, open the command prompt and type:
The > character tells the console to output STDOUT to the file with the name you’ve provided.
When you run this command, you’ll notice that there isn’t any response in the command window except the error that the file doesn’t exist.
This is because the standard output for the command was redirected to a file called myoutput.txt. The file now exists in the same directory where you ran the command. The standard error output still displays as it normally does.
Note: Be careful to change the active directory for the command prompt before running the command. This way you’ll know where the output files are stored.
You can view the standard output that went to the file by typing “myoutput.txt” in the command window. This will open the text file in your default text file viewer. For most people, this is usually Notepad.exe.
The next time you run the same command, the previous output file will be deleted. A new output file will be recreated with the latest command’s output.
Redirect Standard Output Writes to the Same File
What if you don’t want to overwrite the same file? Another option is to use >> rather than > to redirect to an output file. In the case of this example, you would type:
You’ll see the same output (the error only).
But in this case, instead of overwriting the output file, this command appends the new output to the existing output file.
Every time you run a command and append the output to a file, it’ll write the new standard output to the end of the existing file.
Redirect Standard Error To a File
The same way you can redirect standard output writes to a file, you can also output the standard error stream to a file.
To do this, you’ll need to add 2> to the end of the command, followed by the output error file you want to create.
In this example, you’ll type the command:
This sends the standard output stream to myoutput.txt, and the standard error stream to output.err. The result is that no output stream at all gets displayed in the console window.
However, you can see the error messages by typing output.err. This will open the file in your default text file viewer.
As you can see, any error messages from the command are output to the error file. Just as with the standard output, you can use >> instead to append the error to errors from previously run commands.
Redirect All Output Writes to a Same File
All of the approaches above result in multiple files. One file is for the standard output stream and the other is for the standard error stream.
If you want to include both of these outputs to the same file, you can do that too. To do this, you just need to redirect all output to the same file using the following command.
Here’s how this command works:
- The standard output is directed to the output file identified by output number 1.
- The standard error output identified by the number 2 is redirected to the output file identified by number 1.
This will append the error output to the end of the standard output.
This is a useful way to see all output for any command in one file.
Silencing Standard or Error Output Streams
You can also turn off either Standard Output or Standard Error by redirecting the output to a NUL instead of a file.
Using the example above, if you only want Standard Output and no Standard Error at all, you can use the following command:
This will result in the same output file as the first example above where you only redirected the Standard Output, but with this command the error won’t echo inside the console. It won’t create an error log file either.
This is useful if you don’t care about any errors and don’t want them to become a nuisance.
You can perform any of the same output commands above from inside a BAT file and the output from that line will go to the output file you specify. This is a useful way to see whether any commands within a BAT file had any errors when they tried to run.
Ryan has been writing how-to and other technology-based articles online since 2007. He has a BSc degree in Electrical Engineering and he’s worked 13 years in automation engineering, 5 years in IT, and now is an Apps Engineer. Read Ryan’s Full Bio
How to Redirect Command Output to a File
Use redirection operators to save a command’s results to a file
Use a redirection operator to redirect the output of a command to a file. It’s one of our favorite Command Prompt Tricks & Hacks.
All the information that’s displayed in the Command Prompt after running a command can instead be saved to a file which you can open in Windows to reference later or manipulate however you like.
While there are several redirection operators, which you can read in detail about here, two, in particular, are used to output the results of a command to a file: the greater-than sign, >, and the double greater-than sign, >>.
How to Use Redirection Operators
The easiest way to learn how to use these redirection operators is to see some examples:
In this example, all the network configuration information normally seen on screen after running ipconfig /all, is saved to a file by the name of mynetworksettings.txt. It’s stored in the folder to the left of the command, C:\Users\jonfi in this case.
The > redirection operator goes between the ipconfig command and the name of the file. If the file already exists, it’ll be overwritten. If it doesn’t already exist, it will be created.
Although a file will be created if doesn’t already exist, folders will not. To save the command output to a file in a specific folder that doesn’t yet exist, first, create the folder and then run the command. Make folders without leaving Command Prompt with the mkdir command.
Here, when the ping command is executed, Command Prompt outputs the results to a file by the name of Ping Results.txt located on the jonfi user’s desktop, which is at C:\Users\jonfi\Desktop. The entire file path in wrapped in quotes because there was a space involved.
Remember, when using the > redirection operator, the file specified is created if it doesn’t already exist and is overwritten if it does exist.
The Append Redirection Operator
The double-arrow operator appends, rather than replaces, a file:
This example uses the >> redirection operator which functions in much the same way as the > operator, only instead of overwriting the output file if it exists, it appends the command output to the end of the file.
Here’s an example of what this LOG file might look like after a command has been exported to it:
The >> redirection operator is useful when you’re collecting similar information from different computers or commands and you’d like all of that data in a single file.
The above redirection operator examples are within the context of Command Prompt, but you can also use them in a BAT file. When you use a BAT file to pipe a command’s output to a text file, the exact same commands described above are used, but instead of pressing Enter to run them, you just have to open the .BAT file.
Use Redirection Operators in Batch Files
Redirection operators work in batch files by including the command just as you would from the Command Prompt:
The above is an example of how to make a batch file that uses a redirection operator with the tracert command.
Redirect Windows cmd stdout and stderr to a single file
I’m trying to redirect all output (stdout + stderr) of a DOS command to a single file:
Is it possible, or should I just redirect to two separate files?
7 Answers 7
The syntax 2>&1 will redirect 2 (stderr) to 1 (stdout). You can also hide messages by redirecting to NUL , more explanation and examples on MSDN.
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.
Background info from MSKB
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 google to find out what was actually going on. In the hopes that this information is helpful to others, I’m posting it here.
From MSKB110930
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 which is STDERR.
Example
The command dir file.xxx (where file.xxx does not exist) will display the following output:
If you redirect the output to the NUL device using dir file.xxx > nul , you will still see the error message part of the output, like this:
To redirect (only) the error message to NUL , use the following command:
Or, you can redirect the output to one place, and the errors to another.
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:
How do you redirect standard input to a file in the Windows command line?
On Unix I would do something like:
How can I do this on the Windows command prompt or batch file?
EDIT: Basically, I am looking for the functionality of cat with no arguments (it reads from stdin and spits it back out to stdout).
4 Answers 4
CON is the MS-DOS device for console input. You can redirect to a file as follows:
To terminate, hit Ctrl + C or Ctrl + Z , Enter ( Ctrl + Z = EOF).
If all you want is to read stdin and write what you read to stdout, then FINDSTR may work, depending on how you use it.
FINDSTR will output an exact binary image of the input as long as the input is specified as a single file name at the end of the argument list.
Pipes or redirection may also work, depending on the content of the input:
The output will be corrupted if any of the following occur while using redirected or piped input with FINDSTR:
- Any input line > 8191 bytes
- Last line of input is not terminated by \n. (command may hang if redirected input)
FINDSTR will not work if multiple input files are specified because in that case the name of the file will be used as a prefix to each line of output.
FINDSTR also differs from cat in that it cannot read from both stdin and a named file.