Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Judago
This post related to windows Xp and batch scripts.
I'm trying to get stderr (e.g. 2>) directly into a variable to avoid loosing information if a file is locked(eg. net helpmsg 32).The batch I have in mind generates numbers that have no two digits the same like 12345 or 53216 and appends them to a textfile. If the file is locked I loose any appended information. I want to set a variable to the output of stderr so I can output the number again if the command failed.
so basically the command is along the lines of:
echo:%var1%%var2%%var3%>>textfile
I'm looking for something along the lines of:
(echo:%var1%%var2%%var3%>>textfile) 2>(??what goes here??)I know I could output 2> into a text file and either use for /f to get the error(if any) into a variable or call a label with the file name as an argument and use %~z<arg>, but I would much prefer using a variable for speed and I try to avoid these sorts of temp files where possible.
This might even be the question "is this even possible?". I've tried many thing like piping to set commands ect. to no avail. I know it may seem picky but it's something i've been wondering for a while....
Any help is much appreciated.

If you just want the last line of STDERR:
FOR /F "delims=" %%a IN ('??? 2^>^&1 1^>NUL') DO @SET errMsg=%%a
I'd give you the exact answer, but since I'm in the middle of an extended blackout, you only get what I can pull from memory. (Hint: You'll probably need to use ^|FIND as well.)On a completely unrelated note, I can't find any help in WinXP about the old DOS devices, like NUL, CON, PRN, etc.

I don't seem to be able to make it work for my purposes. It does work with other commands, but the fact I'm trying to redirect stdout into a text file seems to stop it working. I think it's the fact that the command isn't failing, the redirection is.
The folowing line acts as though the whole for command spits out the error when 9dig.txt is locked and doesn't set errmsg under any circumstances I encountered.
FOR /F "DELIMS=" %%A IN ('ECHO:%AAA%%AAB%%AAC% 2^>^&1 1^>^>D:\9DIG.TXT') DO SET ERRMSG=%%A
Any ideas?

Yeesh, you're trying to ECHO.
Ahem...
Your problem is in the origin of the message; as ECHO is a built in, the message comes from the shell itself (CMD), and not the command (ECHO).The solution is to wrap up the echo into another shell, and go from there: (Warning: Not pretty)
FOR /F "delims=" %%a IN ('CMD /C "ECHO:%AAA% %AAB% %AAC% >>D:\9DIG.TXT" 2^>^&1') DO @set errMsg=%%a
On the positive side, this is about as convoluted as command scripting gets. (Until you attempt date math, at least.)

Thank's for that, works just how I want it to. I wouldn't have got it without being told. Another trick for the bag is always useful.
Now how does that rhyme go:Thirty days has September.............

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |