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

By neokrates, written on November 10, 2010

howto

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 4,897 views
Rate it
Ad
Poll
  • Your favorite Java IDE is?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Works for:

✔ Windows XP cmd shell

Should also work for:

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

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: -7 (from 19 votes)
6 votes 'YES'  13 votes 'NO'


TAGS

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS


One Response to “Assign command output to variable in dos .bat (cmd) shell”

  1. […] Assign command output to variable in dos .bat (cmd) shell – Assign command output to variable in dos .bat (cmd) shell By neokrates, written on November 10, 2010: howto […]

Leave a Reply