|
Answer» 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 use findstr for 2 or more search conditions.
Code: [Select]D:\batch>date The current date is: 27/05/2009 Enter the new date: (dd-mm-yy)
D:\batch>(dir not 2>&1 & dir *.ini 2>&1)|findstr/b "[a-z] [0-9]*/[0-9]*/2009" File Not Found 17/05/2009 19:50 61 NOTES.INI adjust findstr search condition to match your date formatThank you Reno
I was aware of Findstr a few years ago, and at the time I found no benefit in the extra complexity.
I now see the benefit, and your solution fully meets my immediate needs.
In the past I have done many strange things with various piped commands other than DIR and FIND. I am sure I will continue to do so ! ! For the future, I would appreciate any further advice on the use of 2>&1 or related mechanisms.
Again, thank you Reno
Regards Alan Feedback :-
I have found a solution to the error "The process cannot access the file because it is being used by another process."
The first line of code below produces the error; The error is cured by adding brackets as in the second line Code: [Select]DIR *.TXT 2>> RESULT.TXT | FIND "/2009" >> RESULT.TXT DIR *.TXT 2>> RESULT.TXT |( FIND "/2009" >> RESULT.TXT )
Regards Alan
|