Output/Redirection Question 1> and 2>

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
squadjot
Posts: 4
Joined: 17 Dec 2009 13:14

Output/Redirection Question 1> and 2>

#1 Post by squadjot » 17 Dec 2009 13:30

Hi, i have one more question =)

I use the > syntax to output the console to a file.

The thing is, in some cases i have to use 2> to catch errors.

Does anybody know how i catch BOTH non-error -and error-messages?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 17 Dec 2009 23:48

squadjot,

redirect stderr into stdout in other words:
redirect 2 into &1 like this:

command >file.txt 2>&1

Note: the order you put the redirection is important:
command >file.txt 2>&1 :) GOOD
command 2>&1 >file.txt :( BAD

Some examples:

Code: Select all

C:\>(echo.good & set /A)
good
The syntax of the command is incorrect.

C:\>(echo.good & set /A) >NUL
The syntax of the command is incorrect.

C:\>(echo.good & set /A) 2>NUL
good

C:\>(echo.good & set /A) >NUL 2>&1

C:\>(echo.good & set /A) >aa.txt 2>&1

C:\>type aa.txt
good
The syntax of the command is incorrect.

And this won't not work:

Code: Select all

C:\>(echo.good & set /A) 2>&1 >aa.txt
The syntax of the command is incorrect.

C:\>(echo.good & set /A) 2>aa.txt 1>aa.txt
The process cannot access the file because it is being used by another process.

DosItHelp? :wink:

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 18 Dec 2009 15:48

I'd like to note that the >nul is a special case, where you CAN do this:

(echo.good & set /A) >nul 2>nul

But if you're redirecting to a file, you'll get the "file in use" error, and as such need the >&1 notation as noted above.

Same rules for append >>a.txt 2>>&1

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re:

#4 Post by alan_b » 22 Jan 2010 05:39

DosItHelp wrote:
command >file.txt 2>&1 :) GOOD
command 2>&1 >file.txt :( BAD



BAD is worse than GOOD, but there is BETTER ! !

I found that sometimes the two streams collided and file.txt missed the data,
and instead I got an error.

I found it was necessary to redirect one stream to the log file,
and the other stream to a temporary file,
after which the temporary file was appended to the log file.

Regards
Alan

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Output/Redirection Question 1> and 2>

#5 Post by avery_larry » 22 Jan 2010 10:54

I've never seen an error with 1>file.txt 2>&1

Problem with your method -- the lines will be out of order. But if that's the only way to catch everything . . .

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Output/Redirection Question 1> and 2>

#6 Post by alan_b » 22 Jan 2010 13:11

There is no problem with the message order.

Normally a command may give only a response via either STDOUT or STDERR,
in which case there is no possibility of the wrong order.

Under some conditions some commands may result in a response from both,
in which case the order in which they appear is under control of my script.
I accept that my script will always present my chosen order instead of the order in which they would have appeared on the screen had there been no redirection, but I prefer consistent order instead of the random chaos of sundry race hazards.

N.B. I cannot remember the precise details of how STDOUT and STDERR occur "simultaneously",
but I think it involved piping output from one command through another, such as FIND.EXE.

Alan

Post Reply