- Writing command output in Windows cmd to a file (with a twist)
- 1 Answer 1
- How do you redirect standard input to a file in the Windows command line?
- 4 Answers 4
- 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 I echo and send console output to a file in a bat script?
- 12 Answers 12
- Windows BAT or CMD: send some data to a localhost udp port
- 3 Answers 3
Writing command output in Windows cmd to a file (with a twist)
So I’m trying to run foo.exe , but I don’t want the output to the terminal but into a file. Running foo.exe > foo.txt should accomplish this for me, but it’s not. When I’m running the exe-file, I get the output. The exe is working fine in other words. However, when I try to send the output to a file, the only thing I get is this:
This only shows up when I try to send it to a file. Thinking that it could be the path (which is c:\Program Files (x86)\ and so on) which is misinterpreted, I tried specifying the output file like so: foo.exe > c:\test.txt , but still no joy.
So, aside from stating that the binary that I am trying to run is poorly written, is there anything I can do to remedy this? Keep in mind that I do get valid output when simply running the exe, it just won’t print nicely to a file. Obviously the output is there, the question is if there is some way to catch it.
1 Answer 1
You haven’t shown the command you are using that is failing. If you show it in your question, it might be easier to find a solution for you.
I expect your command is something like this:
C:\> foo.exe|c:\Program Files (x86)\something\test.txt
The error you are receiving is somewhat of a clue:
‘c:/Program’ is not recognized as an internal or external command, operable program or batch file.
First:
. is not recognized as an internal or external command, operable program or batch file.
This typically happens when you try to redirect to a file using a | instead of a > .
Second:
‘c:/Program’ .
When specifying a filename (or path) that contains spaces, you must surround it in double-quote marks ( «. « ) . This is because when the OS is determining the file to redirect to, it will stop looking for the filename when it encounters an unquoted space: «c:/Program» .
If the above doesn’t work to capture the output from foo.exe to the text file, then there is another possibility.
If the program foo.exe is writing its output to STDERR instead of STDOUT , the output of foo.exe will not be captured by using simple redirection with a single > . You would have to do it like this:
Edit:
Here is an explanation of file redirection and the 2>&1 notation.
When a program writes to the terminal, it can write to one of two Streams .
Stream 1 is referred to as STDOUT or Standard-Output. Typically, programs write their «Normal» output to stream 1.
Stream 2 is referred to as STDERR or Standard-Error. Typically, programs write their «Error» output (error and warning messages) to stream 2.
Whether a program writes a particular output to STDOUT or STDERR is determined by the programmer and how they wrote the program. Some programs are written to send all output (normal output and errors) to STDOUT .
When a program is run with no output redirection, all normal and error output is sent to the terminal screen without any distinction between what is STDOUT output or STDERR output.
When you do «normal» redirection with a single > like this:
you are not specifying which Stream is being redirected to the file, so Stream 1 is assumed.
It’s the same as if you typed it like this:
This tells the command interpreter ( cmd.exe ) to capture the program output for STDOUT (Stream 1) to the specified filename. The 1 in 1> refers to Stream 1.
In this case all the normal program is captured to the file, but if the program writes to STDERR (Stream 2), that output will not be captured and will be shown on the screen. This is generally the «desired» way to do it so that while you are capturing the normal program output, you can see on the screen if an error occurs.
If you want to capture «Normal» output to one file, and «Error» output to a different file you can do it like this:
If you want the «Normal» output and the «Error» output to be captured to the same file, you can specify it like this:
This is basically a «shorthand» way of specifying it and it means to redirect Stream 1 to the specified file, and to also redirect Stream 2 to the same «place» (file) as Stream 1.
Edit:
Is there any difference between foo.exe > «c:\output.txt» 2>&1 and foo.exe > «c:\output.txt» 2>»c:\output.txt»? Are they identical?
Short answer: You would think they are identical, but no. They are different.
With redirection using >»filename.ext» , 1>»filename.ext» , or 2>»filename.ext» , the > causes the output to be written to a new file named «filename.ext». If the file «filename.ext» already exists, it will be deleted first.
foo.exe > «c:\output.txt» 2>»c:\output.txt»
causes a «conflict» where both redirections are trying to write to the same file and both are trying to delete the file if it already exists. This will likely cause undesired behavior. Generally, one or the other, or both, of the outputs will NOT be captured fully, or predictably.
The actual result will depend on the operating system and version, and may also depend on the command being executed. What will likely happen is:
1 The output sent to one of the redirections will be captured or partially captured, and the output sent to other redirection will be lost. 2 The operating system will complain about the command and neither of the outputs will be captured (fully). 3 Undefined, undesired, unpredictable, unexpected behavior.
On Windows 7 and likely on Windows Vista/8/10, and possibly on Windows XP, the operating system will complain about command and the command will be canceled.
For example (Windows 7): I have a folder named: «C:\Temp\emptyfolder» and a file named «nonexistantfile» doesn’t exist there.
In this case, using one redirection ( >output.txt ), the output of the dir command is captured the the file: output.txt , and the error message File Not Found is shown on the screen. this is the expected behavior.
Now, using both redirections («>file» AND «2>file»):
In this case, the operating system complained that the (outout) file is already in use. And the file «output.txt» ends up empty (0 bytes), and the output for both of the redirections was lost.
Now, lastly, using both redirections («>file» AND «2>&1»):
In this case, «>file» causes the output for «stream 1» («standard output») to be captured to the file. And «2>&1» causes the output for «stream 2» («error output») to be sent through the already redirected «stream 1», and to also be captured to the (same) file.
It is also worth noting that the order is important. Reversing the order like this:
is not the same and will probably not give you the desired result.
In this case, «2>&1», which is seen and precessed first, causes the output for «stream 2» («error output») to be redirected to the place where «stream 1» is currently directed to, which at that moment, is (by default), the screen. And «>file» causes the output for «stream 1» («standard output») to be captured to the file. The end result, is that the output of the command («stream 1») will be captured to the file, but the error output («stream 2»), will still go to the screen (not to the 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.
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 I echo and send console output to a file in a bat script?
I have a batch script that executes a task and sends the output to a text file. Is there a way to have the output show on the console window as well?
Is there a way to have the output of dir display in the console window as well as put it into the text file?
12 Answers 12
No, you can’t with pure redirection.
But with some tricks (like tee.bat) you can.
I try to explain the redirection a bit.
You redirect one of the ten streams with > file or , this means the stream 1 (STDOUT) will be redirected.
So you can redirect any stream with prepending the number like 2> err.txt and it is also allowed to redirect multiple streams in one line.
In this example the «standard output» will go into files.txt, all errors will be in err.txt and the stream3 will go into nothing.txt (DIR doesn’t use the stream 3).
Stream0 is STDIN
Stream1 is STDOUT
Stream2 is STDERR
Stream3-9 are not used
But what happens if you try to redirect the same stream multiple times?
«There can be only one», and it is always the last one!
So it is equal to dir > two.txt
Ok, there is one extra possibility, redirecting a stream to another stream.
2>&1 redirects stream2 to stream1 and 1>files.txt redirects all to files.txt.
The order is important here!
are different. The first one redirects all (STDOUT and STDERR) to NUL,
but the second line redirects the STDOUT to NUL and STDERR to the «empty» STDOUT.
As one conclusion, it is obvious why the examples of Otávio Décio and andynormancx can’t work.
Both try to redirect stream1 two times, but «There can be only one», and it’s always the last one.
So you get
And in the first sample redirecting of stream1 to stream1 is not allowed (and not very useful).
Windows BAT or CMD: send some data to a localhost udp port
I have an app that listens on a localhost port. I want a minimal launcher to bring that app’s to the front.
My app is in Java. I don’t know how to write any exe files, but I can write bat files, and then make a shortcut that launches the bat file without showing the terminal. I would like to have the bat file send some data to a port (preferably UDP so there will not be any stream overhead).
How can I send data to a UDP port?
One of the things I thought of was nslookup , but that will only connect to port 53 as far as I know.
This only needs to work on Windows. Would like it to work on XP and up, but I can make a workaround if necessary.
3 Answers 3
This is a job for netcat.
After installing you can easily launch it from the command line or write a BAT script to execute it for you. To send date through UDP rather than TCP, use the «-u» switch.
For example, to send the data to UPD port 2345 on localhost execute:
Then type the data you want to send.
I was having quite a few problems sending a UDP command to a Brightsign digital sign with netcat. I ended up using Swiss File Knife. It is possible I had the syntex wrong with my netcat statement. (If anyone could suggest the correct syntax based on my working SFK statement, I would love to know) I just wanted to send a few asci key words to my sign from a batch file. My sign was 192.168.1.22 and listening on port 5000
Here are my working steps:
Copied the file to C:\tools and renamed the sfk174.exe file to skf.exe(to make is shorter) and used the bellow syntax to send the UDP command Special
C:\tools\sfk udpsend 192.168.1.22 5000 “Special” (make sure you put your asci word in quotes. it will work without but not for words with spaces between)
I put the commands in a batch file and created a windows icon for the customer and they worked great.