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%

35 comments:

Anonymous said...

Thanks, this is very helpful

Brent V said...

Thanks for the feedback! Glad to help.

Thanks,

Brent

Anonymous said...

Wow, that is straight-forward. Albeit lame as all hell that it has to be done that way. You helped me out, thank you.

Brent V said...

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

Anonymous said...

I must say, good work!

Brent V said...

Anonymous,

Thank you for the kind feedback. It is always greatly appreciated.

Thank you,

Brent

Anonymous said...

Thanx alot, this problem would have ruined my weekend :D

Brent V said...

Martin,

So glad you found it helpful and allowed you to enjoy your weekend. :D

Brent

Anonymous said...

very very helpful !! thanks a lot.

Brent V said...

Anonymous,

Thank you for the feedback. So glad it helped.

Brent

Anonymous said...

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

Brent V said...

Anonymous,

That would me nice. :)

Brent

Anonymous said...

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

Brent V said...

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

Anonymous said...

I noticed that if the out has a space in it, it only returns everything before the space. Why is this?

Brent V said...

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

Vesko said...

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.

Anonymous said...

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.

mark said...

Very useful, thanks a lot!

Jose said...

I love you!
All kidding aside it did help, thank you.

Daniel said...

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?

Eric said...

Awesome dude, thanks for the help.

spaceman said...

I often went crazy trying to return a shell command result in a variable (cd, date, ...)
You provided the cure!
Thaks a lot!!!

Anonymous said...

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

Anonymous said...

Using a caret to escape out the pipe was handy info, thankyou!

Anonymous said...

Thank you for sharing!
Excellent article

Anonymous said...

This is an old thread, but THANK YOU!!!!!!!!!!!!!!

Anonymous said...

Thank you! Everything I needed was in here.

Anonymous said...

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?

Anonymous said...

Thanks really helpful all of it. Carrot before pipe and all. Thanks a ton

Anonymous said...

The best answer to this is probably to install Cygwin and use bash or ksh.

Anonymous said...

The best answer to this is probably to install Cygwin and use bash or ksh.

Herm said...

The best answer to this is probably to install Cygwin and use bash or ksh.

Anonymous said...

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

Anonymous said...

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