1.

Solve : Redirect output?

Answer»

Hi,
I am running a DOS command to backup one of my disk files:

xxcopy e:\*.* j:\ /BU/KS/H/E > SKIPFILE.TXT

During the running of this utility it outputs the name of any files that it can't backup (open, locked, etc). I am trying to redirect the output from the screen to a text file. It is not working. The file is created, but nothing is written to it and the output goes to the screen. When it is completed I have an empty file. I am running the utility out of the command prompt window. Also, my OS is Windows 7.

ThanksThere are TWO console output streams, identified by numbers. Stream 1 is called STDOUT and stream 2 is STDERR. Stream 1 is the normal console output and is assumed if no number is used. That is

echo 1> output.txt is equivalent to echo > output.txt

As you might guess STDERR is usually reserved for ERROR messages. To get STDERR into a file using redirection you can redirect stream 2 into the file like this

program.exe 2> errors.txt

Or to get all output you can merge stream 2 and stream 1 and then redirect into a file using this sequence of characters 2>&1.

EXAMPLE:

xcopy bla bla bla > yourfile.txt 2>&1Quote from: Salmon Trout on November 08, 2012, 10:16:49 AM


echo 1>output.txt is equivalent to echo >output.txt


I could have made this clearer

echo hello world >output.txt is equivalent to echo hello world 1>output.txt

If you do this DIR /b *.txt >output.txt you will get a list of any .txt files in the current folder into output.txt BUT if there are none, the error message file not found is output to stream 2 and appears on the screen (because you have not redirected stream 2) so you can do this

valid output to one file, errors to another file:

dir /b *.txt 1>textfiles.list 2>errors.txt

or everything in one file

dir /b *.txt > textfiles.list 2>&1

If you need to echo a string to a file, white space before the redirection symbol is important if the string is a single digit (any digit 0-9) or there might be white space then a single digit number at the end of the string:

C:\>echo my age is 1>test.txt & TYPE test.txt
my age is

C:\>echo my age is 1 >test.txt & type test.txt
my age is 1


Thus echo %var%>file.txt will NOT give the same result as echo %var% >file.txt if %var% is a digit, or ends with a space then a digit

C:\>set var=8

C:\>echo %var%
8

C:\>echo %var%>test.txt
ECHO is on.

C:\>echo %var% >test.txt

C:\>



Hi,
Thanks for responding to my post. But, That didn't work. I did notice there is a wrinkle to the process. I run the utility from the command window, but it opens up another DOS window and the output shows up there. I'm not sure what is going on.

ThanksAh. I didn't read your first post properly. I thought it said xcopy. Now I see you are using xxcopy, (which looks like a regular console program but isn't) I suggest you study the xxcopy documentation (the chm file) (in the downloaded installation zip archive) and the FAQs on the web site especially the logging options, which look like the /Oa and /On switches.


Discussion

No Comment Found