- 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 write console data into a text file in C++?
- 4 Answers 4
- plasmid.co.uk
- 4 thoughts on “Write console text to a file (Windows cmd.exe)”
- How to save command output to file using Command Prompt or PowerShell
- How to save command output to file using Command Prompt
- How to save command output to file using PowerShell
- More Windows 10 resources
- Halo: MCC’s live service elements make it better, not worse
- Microsoft’s Surface Duo is not ‘failing up’
- Here’s what you can do if Windows 10 update KB5001330 is causing issues
- These are the best PC sticks when you’re on the move
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 write console data into a text file in C++?
I’m working on a file sharing application in C++. I want to write console output into a separate file and at the same time I want to see the output in console also. Can anybody help me. Thanks in advance.
4 Answers 4
i don’t program in c++ but here is my advice: create new class, that takes InputStream (istream in c++ or smth), and than every incoming byte it will transfer in std.out and in file.
I am sure there is a way to change standard output stream with forementioned class. As i remember, std.out is some kind of property of cout.
And again, i spent 1 week on c++ more than half a year ago, so there is a chance that all i’ve said is garbage.
PS: OK, read your question wrong (console output/input mixup), but you still get the idea I think.
The idea is to create a derivate of std::streambuf which will output data to both the file and cout. Then create an instance of it and use cout.rdbuf(. );
Here is the code (tested with MSVC++ 2010, should work on any compiler):
However note that a better implementation would use templates, a list of streambufs instead of just two, etc. but I wanted to keep it as simple as possible.
What you want actually is to follow in real time the lines added to the log your application writes.
In the Unix world, there’s a simple tool that has that very function, it’s called tail .
Call tail -f your_file and you will see the file contents appearing in almost real time in the console.
Unfortunately, tail is not a standard tool in Windows (which I suppose you’re using, according to your question’s tags). It can however be found in the GnuWin32 package, as well as MSYS.
There are also several native tools for Windows with the same functionality, I’m personally using Tail For Win32, which is licensed under the GPL.
So, to conclude, I think your program should not output the same data to different streams, as it might slow it down without real benefits, while there are established tools that have been designed specifically to solve that problem, without the need to develop anything.
plasmid.co.uk
Writing directly from the Windows command line to a file might not be something you need to do very often. The famous black and white console is better suited to serving system commands and file operations than being a stand-in for notepad. However, it is sometimes useful to have the option of writing multiple lines of text to a file directly from the trusty prompt.
Method One
This is suitable for creating/overwriting a file with multiple lines of text.
copy con SOME_FILE.txt
Type your text here
You can even have multiple lines!
When finished, press CTRL+Z to confirm your action (or CTRL+C to cancel) and ENTER.
If you want to append some text instead of overwriting it completely, follow as above but using:
copy SOME_FILE.txt + con
Method Two
Open up the command prompt and type:
echo SOME TEXT > SOME_FILE.txt
This method creates/overwrites SOME_FILE.txt with the text you entered before the > . It is only suitable for entering a single line of text into a file.
As with method one, it possible to append text using >> instead of > in the command.
4 thoughts on “Write console text to a file (Windows cmd.exe)”
Thanks a lot. This command is not new for me because I’m deling with solaris and linux but I nevar thought this will work with windows. Anyway thank you!!
Thank you very much for your input, as I am new to this could you please suggest me some command to stop the writting in file. I want to use this iin batch file. Thank you.
but if i want to do this
he wreite “if(a” in a file called “5) >>”
help me xD
How to save command output to file using Command Prompt or PowerShell
On Windows 10, the ability to save the output of a PowerShell or Command Prompt command to a text file can come in handy in many scenarios. For example, when you’re troubleshooting a problem, it’s a convenient way to export and analyze the command output, or you can share the output with someone else who can help. You can print a command output to a text file to save system configurations for documentation purposes, and more.
Although you could just select the content, right-click the selection to copy the content to the clipboard, and then paste on a text file, this method requires additional steps, which you can avoid with a single command-line using the redirect output to file function.
In this Windows 10 guide, we’ll walk you through the steps to save a command output to a text file whether you’re using Command Prompt or PowerShell.
How to save command output to file using Command Prompt
To save a command output to a text file using Command Prompt, use these steps:
- Open Start.
- Search for Command Prompt, right-click the top result, and select the Run as administrator option.
Type the following command to save the output to a text file and press Enter:
In the command make sure to replace «YOUR-COMMAND» with your command-line and «c:\PATH\TO\FOLDER\OUTPUT.txt» with the path and file name to store the output.
(Optional) If you want to save the output, and view the result on the screen, then use this command and press Enter:
YOUR-COMMAND > c:\PATH\TO\FOLDER\OUTPUT.txt | type c:\PATH\TO\FOLDER\OUTPUT.txt
In the command make sure to replace «YOUR-COMMAND» with your command-line and «c:\PATH\TO\FOLDER\OUTPUT.txt» with the path and filename to store and view the output.
Quick tip: If you’re having issues viewing the file, you can use the type c:\PATH\TO\FOLDER\OUTPUT.txt command after step No. 3.
Once you complete the steps, the command output will be saved into a text file, which you can then review or share with tech support.
How to save command output to file using PowerShell
To save a command output to a text file using PowerShell, use these steps:
- Open Start.
- Search for PowerShell, right-click the top result, and select the Run as administrator option.
Type the following command to save the output to a text file and press Enter:
YOUR-COMMAND | Out-File -FilePath c:\PATH\TO\FOLDER\OUTPUT.txt
In the command make sure to replace «YOUR-COMMAND» with the command-line that you want and «c:\PATH\TO\FOLDER\OUTPUT.txt» with the path and file name to store the output.
(Optional) Type the following command to view the saved output on the screen and press Enter:
Get-Content -Path c:\PATH\TO\FOLDER\OUTPUT.txt
In the command make sure to replace «c:\PATH\TO\FOLDER\OUTPUT.txt» with the path and file name with the output content.
After completing the steps, the PowerShell command will save the result into a text file on the location that you specified.
We’re focusing this guide on Windows 10, but you can also use these instructions on Windows 8.1 and Windows 7.
More Windows 10 resources
For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:
Halo: MCC’s live service elements make it better, not worse
Halo: The Master Chief Collection is more popular than ever, but some fans don’t agree with the live service approach 343 Industries has taken with it. Here’s why those elements are, at the end of the day, great for the game and for Halo overall.
Microsoft’s Surface Duo is not ‘failing up’
Microsoft announced this week that it was expanding Surface Duo availability to nine new commercial markets. While Surface Duo is undoubtedly a work in progress, this is not a sign of a disaster. It’s also doesn’t mean that Surface Duo is selling a ton either. Instead, the reason for the expansion is a lot more straightforward.
Here’s what you can do if Windows 10 update KB5001330 is causing issues
In this guide, we’ll show you the steps to get rid of the update KB5001330 to fix profile, gaming, and BSoD problems with the Windows 10 October 2020 Update and May 2020 Update.
These are the best PC sticks when you’re on the move
Instant computer — just add a screen. That’s the general idea behind the ultra-portable PC, but it can be hard to know which one you want. Relax, we have you covered!