Computing.Net > Forums > Programming > batch stderr directly into variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch stderr directly into variable

Reply to Message Icon

Name: Judago
Date: September 17, 2008 at 02:29:46 Pacific
OS: Xp home sp3
CPU/Ram: Athlon x2 64 5600+/2gb
Product: Me - K9A2 CF-F
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: September 17, 2008 at 14:45:39 Pacific
Reply:

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.


0

Response Number 2
Name: Judago
Date: September 18, 2008 at 02:01:48 Pacific
Reply:

Thanks for the input I'll give it a good look into. I post back when I do some proper testing.


0

Response Number 3
Name: Judago
Date: September 19, 2008 at 15:20:50 Pacific
Reply:

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?


0

Response Number 4
Name: Judago
Date: September 20, 2008 at 17:43:06 Pacific
Reply:

I forgot to mention there is a little help for con under:

mode /?


0

Response Number 5
Name: Razor2.3
Date: September 21, 2008 at 21:30:39 Pacific
Reply:

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.)

0

Related Posts

See More



Response Number 6
Name: Judago
Date: September 22, 2008 at 03:27:19 Pacific
Reply:

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.............


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch stderr directly into variable

Making WinXP load directly into RAM www.computing.net/answers/programming/making-winxp-load-directly-into-ram/8357.html

DOS - batch variables www.computing.net/answers/programming/dos-batch-variables/15058.html

Storing into variables www.computing.net/answers/programming/storing-into-variables/15897.html