When I first saw "2>&1" on this site I thought it was an error, but quickly learnt about this new (to me) facility.
Now I actually want to use it, it fails to work as I expected/hoped for two commands on a line.
DIR *.DOC | FIND "/2009" >> RESULT.TXT
The above appends to a file RESULT.TXT the details of files that match *.DOC,
or alternatively it puts on the console any stderr messages from DIR, such as "File Not Found".
I require that stderr should NOT appear on the console, and really want it to appear in RESULT.TXT.
DIR *.DOC >> RESULT.TXT 2>&1
This works exactly as I learnt/expected from last year.
It works like a charm on stderr - they appear in RESULT.TXT,
BUT RESULT.TXT also gets all the files from last year.
DIR *.TXT | FIND "/2009" >> RESULT.TXT 2>&1
If all is well then desired files (dated this year) are listed in RESULT.TXT.
If FIND is able to create stderr, that also may appear in RESULT.TXT
BUT UNFORTUNATELY any stderr from DIR now goes to console.
DIR *.TXT 2>&1 | FIND "/2009" >> RESULT.TXT
I "took a shot" and it worked, the console remained clean,
UNFORTUNATELY stderr from DIR was blocked by FIND from getting to RESULT.TXT
DIR *.TXT 2>> RESULT.TXT | FIND "/2009" >> RESULT.TXT
I "took a shot", but was rewarded on the console with
"The process cannot access the file because it is being used by another process."
IF EXIST ERROR.TXT DEL ERROR.TXT
DIR *.TXT 2> ERROR.TXT | FIND "/2009" >> RESULT.TXT
IF EXIST ERROR.TXT TYPE ERROR.TXT >> RESULT.TXT
That should achieve the end result, but I hate all the extra complexity of two additional command lines, each with multiple commands, and it introduces more possibilities for stderr appearing on the screen (e.g. if ERROR.TXT is write protected, or if pernicous "permissions" prevent successful access by an non-admin user.)
Advice upon improving the above code would be helpful for now, but I would really appreciate advice that is not specific to only "DIR | FIND ", but can be applied to any two (or more) "piped" commands
Regards
Alan