Assign command output to variable in dos .bat (cmd) shell

By neokrates, written on November 10, 2010

howto

Rate it
  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
Ad
Poll
  • Your favorite way to manage packages is?

    • Something else (100%, 9 Votes)
    • Tarball command Line (0%, 0 Votes)
    • Debian Dpkg command Line (0%, 0 Votes)
    • Manually (0%, 0 Votes)
    • Synaptic (0%, 0 Votes)
    • Adept (0%, 0 Votes)
    • Apt-get command line (0%, 0 Votes)

    Total Voters: 9

    Vote

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext
Most popular search terms:

Works for:

[v] Windows XP cmd shell

Should also work for:

[v] any cmd or command shell

I have two unix-like tools which can extend my scripting under DOS, here::

c:\test > dir
13.10.2010  08:51            39.424 date.exe
13.10.2010  08:51            57.856 ls.exe
10.11.2010  18:00               535 test.bat

 

What I want is to assign the standard command output from these tools to variable for further processing. Here is one way to do that:

@echo off
echo.
echo.
call :_DATE
echo [DATE]:
echo %result%
echo.

call :LS_FILE
echo [LS]:
echo %result%
echo.

goto :EOF

:_DATE
FOR /F "tokens=1 delims=" %%A in ('date.exe') do SET result=%%A
GOTO :eof

:LS_FILE
FOR /F "tokens=1 delims=" %%A in ('ls.exe --full-time -l %*') do SET result=%%A
GOTO :eof

 
Have fun! :)

 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: -7 (from 11 votes)
2 votes 'YES'  9 votes 'NO'

LEARN MORE (amazon bookstore)

TAGS

RELATED
Pages
Posts
    nope :(

SOCIAL
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • Blogosphere News
  • E-mail this story to a friend!
  • Furl
  • LinkArena
  • Live
  • MisterWong
  • Print this article!
  • StumbleUpon
  • Technorati
  • Webnews.de
  • YahooMyWeb

INCOMING SEARCH TERMS


Leave a Reply