Tuesday, July 29, 2008
Save output of DOS command to variable
What I want to do is something like:
set myVar=pwd
BASH and other *nix shells allow you to do things like that (yes the syntax is different) and it is built into the shell. I find it unbelievable that it is so difficult in a batch file to save the output of a DOS command to a variable that can be used later in the script. I know you can use redirection to a file, call the file, delete the file, etc, but that just seems really lame. Then I found that the for loop will allow me to do what I want. It is truly a hack, but it does work, and is fairly straight forward.
FOR /F "tokens=1 delims=" %%A in ('pwd') do SET myVar=%%A
echo %myVar%
if you are not in a batch file you can test it using
FOR /F "tokens=1 delims=" %A in ('pwd') do SET myVar=%A
echo %myVar%
Subscribe to:
Post Comments (Atom)
35 comments:
Thanks, this is very helpful
Thanks for the feedback! Glad to help.
Thanks,
Brent
Wow, that is straight-forward. Albeit lame as all hell that it has to be done that way. You helped me out, thank you.
Hi Gregory,
Yeah... it is a very lame way to have to do it, but I was happy when I figured out that I could at least do it. Thanks for the feedback.
Brent
I must say, good work!
Anonymous,
Thank you for the kind feedback. It is always greatly appreciated.
Thank you,
Brent
Thanx alot, this problem would have ruined my weekend :D
Martin,
So glad you found it helpful and allowed you to enjoy your weekend. :D
Brent
very very helpful !! thanks a lot.
Anonymous,
Thank you for the feedback. So glad it helped.
Brent
Thanks, very cool.
Now what if your command was a piped command (multi command solution)?
i.e. systeminfo | find /I "system up time"
Thanks again...
Anonymous,
That would me nice. :)
Brent
Excellent description (it was good to hear that this really is a hard thing to do in DOS), good solution, and great description of solution (lame but very effective!). Many thanks!!
Anonymous,
Agreed, this is definitely a lame solution, but the only one I know of. I was shocked how difficult and feature lacking dos really is.
Thank you for the feedback.
Cheers,
Brent
I noticed that if the out has a space in it, it only returns everything before the space. Why is this?
Hi anonymous,
I'm not sure what you mean. If I do something that has a space in the output of the command I think it works fine for me, but maybe you can clarify your problem.
Here is what I tried.
FOR /F "tokens=1 delims=" %A in ('date /t') do SET myVar=%A
echo %myVar%
In my case, I got Tue 11/10/2009 as the output
I hope this is helpful.
Brent
FOR /F "tokens=1 delims=" %%A in ('ping -n 1 www.google.com | find "Reply"') do SET PING=%%A
gives me that error message:
| was unexpected at this time.
When you use a pipe you need to prefix it with a caret ^.
.. %%i in (`wget -q -O- %FEED% ^| sed -n -f rss.sed`) do ..
but unlike BASH it seems that you can't store multiline variables and are forced to use temp files.
Very useful, thanks a lot!
I love you!
All kidding aside it did help, thank you.
I am trying to run:
FOR /F "tokens=1 delims=" %%A in ('A:\BIN\SED -f A:\IPSCR.SED A:\IP1.TXT') DO SET IPADR=%%A
in a batch file. I get a syntax error after this runs. The output from A:\BIN\SED -f A:\IPSCR.SED A:\IP1.TXT is 1921684253, so I don't think that's what's causing the problem.
Does anyone have any ideas?
Awesome dude, thanks for the help.
I often went crazy trying to return a shell command result in a variable (cd, date, ...)
You provided the cure!
Thaks a lot!!!
I was ready to use your solution altough I was sure that I've already used a simple set VAR=%ANTOTHERVAR%.
Then I found it and since your blog is well placed when searching for this problem, I thought I'd better post it here.
set VAR=%CD%
Yeah, right, anything but POSIX compatibility... but it works.
Simon
Using a caret to escape out the pipe was handy info, thankyou!
Thank you for sharing!
Excellent article
This is an old thread, but THANK YOU!!!!!!!!!!!!!!
Thank you! Everything I needed was in here.
Hey, Its "Sets" the final line of the "command" you execute when its of multi-line resulting command.
eg:
FOR /F "tokens=1 delims=" %A in ('dir') do SET myVar=%A
Is there a way we can resolve this?
Thanks really helpful all of it. Carrot before pipe and all. Thanks a ton
The best answer to this is probably to install Cygwin and use bash or ksh.
The best answer to this is probably to install Cygwin and use bash or ksh.
The best answer to this is probably to install Cygwin and use bash or ksh.
Thank you! you save me a lot of time!
PD: it would be nice if you can explain what the for loop is just actually doing and why the trick works :)
the output is this:
ProcessId
8432
I am just interested in the second line.
for /f "tokens=2" %i in
does not work.
Please help
Post a Comment