how to redirect the command output in this case?

T

thinktwice

here me again :), sorry have so many questions, i'm a totally newbie
on cmdprompt.

i know i could redirect the output in this way:
mycommand 2>&1
mycommand 1>log.txt 2>&1
.... (sure i know this from this forum :) )

here is my case

for /F %%i in ('mycommand') do somecommand

i want to redirect the output of mycommand, i have tried
for /F %%i in ('mycommand 2>&1') do somecommand <--------this isn't a
valid command

for /F %%i in ('mycommand') do somecommand 2>&1 <--------seems
redirect the output of "for" command?
 
P

Pegasus \(MVP\)

thinktwice said:
here me again :), sorry have so many questions, i'm a totally newbie
on cmdprompt.

i know i could redirect the output in this way:
mycommand 2>&1
mycommand 1>log.txt 2>&1
... (sure i know this from this forum :) )

here is my case

for /F %%i in ('mycommand') do somecommand

i want to redirect the output of mycommand, i have tried
for /F %%i in ('mycommand 2>&1') do somecommand <--------this isn't a
valid command

for /F %%i in ('mycommand') do somecommand 2>&1 <--------seems
redirect the output of "for" command?

You misunderstand the basic idea of redirection. There are two
redirected output streams: Standard Output and Error Output.
Example:

xcopy c:\*.* d:\Temp\ 1>c:\standard.txt 2>c:\standard.err

All "normal" output from the xcopy command will be written
to standard.txt. If there are any error messages then they will
be written to standard.err.

Some people prefer to have both outputs written to the same
file. This is where the following syntax comes in:

xcopy c:\*.* d:\temp\ 1>c:\standard.txt 2>&1
 
F

foxidrive

i want to redirect the output of mycommand, i have tried
for /F %%i in ('mycommand 2>&1') do somecommand <--------this isn't a
valid command

Within the ('mycommand 2>&1') structure all redirection and pipes must be
escaped by the ^ carot. So ('mycommand 2^>&1') will process both STDOUT as
well as STDERR.
 
T

Tom Lavedas

Within the ('mycommand 2>&1') structure all redirection and pipes must be
escaped by the ^ carot. So ('mycommand 2^>&1') will process both STDOUT as
well as STDERR.

While what you say about the carot is all true, I remain confused
about the original question. Where is the redirection for StdOut
defined? Also, what would the point be of putting it within the set
boundaries? In that location it would completely negate the action of
the FOR /F, wouldn't it? If all of the output from the command inside
of the set part of the FOR is redirected, then what does the DO part
have to work with?

The statement of intent is just not clear enough for me to understand
how to answer thinktwice.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
 
F

foxidrive

While what you say about the carot is all true, I remain confused
about the original question. Where is the redirection for StdOut
defined? Also, what would the point be of putting it within the set
boundaries? In that location it would completely negate the action of
the FOR /F, wouldn't it? If all of the output from the command inside
of the set part of the FOR is redirected, then what does the DO part
have to work with?

The statement of intent is just not clear enough for me to understand
how to answer thinktwice.

It's an interesting thought Tom. Consider this empty M: drive, the DIR
command returns "File Not Found" through STDERR

===[screen capture]===
M:\>dir
Volume in drive M is RAMDISK
Volume Serial Number is 7FFF-FFFF

Directory of M:\

File Not Found

===[screen capture]===

When wrapping it in a for /f command the STDERR is printed first:

===[screen capture]===
M:\>for /f "delims=" %a in ('dir') do @echo %a
File Not Found
Volume in drive M is RAMDISK
Volume Serial Number is 7FFF-FFFF
Directory of M:\

===[screen capture]===

And when I used this syntax the STDERR was printed last (note to the OP
that the & needs to be escaped too):

===[screen capture]===
M:\>for /f "delims=" %a in ('dir 2^>^&1') do @echo %a
Volume in drive M is RAMDISK
Volume Serial Number is 7FFF-FFFF
Directory of M:\
File Not Found

===[screen capture]===

An oddity that may be used to shuffle the order of STDOUT and STDERR but
you're right that it's probably not much use to the OP.
 
F

foxidrive

EDIT: escaped the & too.
Where is the redirection for StdOut defined?

STDOUT defaults to the console or the 1 stream.


In this context ('mycommand 1^>nul') you could capture the STDERR only, as
the for /f captures both STDOUT and STDERR.
 
T

Tom Lavedas

EDIT: escaped the & too.


STDOUT defaults to the console or the 1 stream.

In this context ('mycommand 1^>nul') you could capture the STDERR only, as
the for /f captures both STDOUT and STDERR.

Interesting! A perfectly good use of a really peculiar construct. In
fact, it would also permit the logging to a file of StdOut while
placing the (potential) error message in the environment.

Still, I don't think that is what was being sought.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top