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%

36 comments:

  1. Thanks, this is very helpful

    ReplyDelete
  2. Thanks for the feedback! Glad to help.

    Thanks,

    Brent

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

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

    ReplyDelete
  5. I must say, good work!

    ReplyDelete
  6. Anonymous,

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

    Thank you,

    Brent

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

    ReplyDelete
  8. Martin,

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

    Brent

    ReplyDelete
  9. very very helpful !! thanks a lot.

    ReplyDelete
  10. Anonymous,

    Thank you for the feedback. So glad it helped.

    Brent

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

    ReplyDelete
  12. 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!!

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

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

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

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

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

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

    ReplyDelete
  19. 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?

    ReplyDelete
  20. Awesome dude, thanks for the help.

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

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

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

    ReplyDelete
  24. Thank you for sharing!
    Excellent article

    ReplyDelete
  25. This is an old thread, but THANK YOU!!!!!!!!!!!!!!

    ReplyDelete
  26. Thank you! Everything I needed was in here.

    ReplyDelete
  27. 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?

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

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

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

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

    ReplyDelete
  32. 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 :)

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

    ReplyDelete
  34. FWIW, for future people looking to solve this problem in DOS: the syntax presented is Windows cmd.exe, not DOS command.com. There are no straightforward ways to do this in DOS, unlike in NT-based Windows.

    ReplyDelete