Tom's Hardware > Forum > Windows XP > Windows XP General Discussion > Windows batch file: set output of program to a variable?
Word :    Username :           
 

I'm trying to set the output of a commandline program to a variable in a Windows batch file. For example, if I'd like to read the output of the "ver" command (which tells you what version of window) to a variable called "myvar", how would I do it?

So "ver" outputs for me:

"Microsoft Windows XP [Version 5.1.2600]"

How would I read that into a variable called myvar?

I tried piping it, but can't figure out what the syntax would be.

This is trivial in other languages such as PHP, where I'd simply have:

$myvar = shell_exec("ver" );

Anyway, any help greatly appreciated.

Sponsored Links
Register or log in to remove.

Would myvar be outside the batch file?

If so, you have ver>(location of myvar) without the parens of course; e.g.

ver>c:myvar (You would need the back slash between colon & myvar. We're not allowed to type it.)

Then you could reference it later in the batch file.

Myvar doesn't even have to exist. The redirection would create it.

Reply to pscowboy

If anyone's wondering how to get that variable back into the batch file, here's how:

cmd > tmpFile
set /p myvar= < tmpFile
del tmpFile

(That second line reads back from the temporary file).

Reply to wrybread

to set the output of var into a variable without creating a temp file, go a bit more complex with:

for /f "delims=" %a in ('ver ^| findstr /v "linux"') do @set myvar=%a

check it with:

echo %myvar%

[quote=wrybread]I tried piping it, but can't figure out what the syntax would be. [/quote]
there's your piped command

i know its about three years old... but figured someone could use the help sometime.

Reply to spikejones

Thanks, Spike. I needed that.

Reply to thebuzzerman

Here is another example I wrote using FOR statements that will not require you to output anything to a text file first. This is important if you are working in areas where you might not have write access. I also noticed that you sometimes need to remove spaces like from the Date /T or Ver commands which return something like "Tue 06/20/2009" or "Microsoft Windows Version [6.0.6001]" into separate variables.

@ECHO OFF

:; Clear the screen and turn echo off (above) to keep it clean
CLS

:; Clear any previous variables set
SET Today=
SET DayMonth=
SET MonthDay=
SET Year=
SET Var1=
SET WinVer=
SET WinMajor=
SET WinMinor=
SET WinBuild=
SET WeekDay=
SET DayOfWeek=

:; Get Value from 'VER' command output
FOR /F "tokens=*" %%i in ('VER') do SET WinVer=%%i
FOR /F "tokens=1-3 delims=]-" %%A IN ("%WinVer%" ) DO (
SET Var1=%%A
)

:; Get version number only so drop off Microsoft Windows Version
FOR /F "tokens=1-9 delims=n" %%A IN ("%Var1%" ) DO (
SET WinVer=%%C
echo %WinVer%
)

:; Separate version numbers
FOR /F "tokens=1-8 delims=.-" %%A IN ("%WinVer%" ) DO (
SET WinMajor=%%A
SET WinMinor=%%B
SET WinBuild=%%C
)

:; Fix the extra space left over in the Major
FOR /F "tokens=1 delims= " %%A IN ("%WinMajor%" ) DO (
SET WinMajor=%%A
)

:; Display Results
ECHO WinVer = %WinVer%
ECHO WinMajor = %WinMajor%
ECHO WinMinor = %WinMinor%
ECHO WinBuild = %WinBuild%

:; Pause for a moment
Pause

:; Get the output from the "Date /T"
FOR /F "tokens=*" %%i in ('DATE /T') do SET Today=%%i
FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%" ) DO (
SET DayMonth=%%A
SET MonthDay=%%B
SET Year=%%C
)

:; Separate the Day from Month like "Tue 06" to "Tue"
FOR /F "tokens=1 delims= " %%A IN ("%DayMonth%" ) DO (
SET DayOfWeek=%%A
)

:; Separate the Day from Month like "Tue 06" to "06"
FOR /F "tokens=2 delims= " %%A IN ("%DayMonth%" ) DO (
SET Month=%%A
)

:; Now show the date output using your variables
ECHO Month = %Month%
ECHO DayOfWeek = %DayOfWeek%
ECHO Date = %DayOfWeek% %Month%/%MonthDay%/%Year%

Reply to gary_baird

is that what you meant?

@echo off
setlocal enableextensions
for /f "tokens=*" %%a in (
'VER'
) do (
set myvar=%%a
)
echo/%%myvar%%=%myvar%
pause
endlocal



essentially you can place any exe (along with path), instead of VER, just put it between inverted commas.

Reply to eladkarako

Yes that's correct! I used ver which will display the windows version with something like "Microsoft Windows [Version 6.0.6001]" but you can use any command line application.

Reply to gary_baird
Tom's Hardware > Forum > Windows XP > Windows XP General Discussion > Windows batch file: set output of program to a variable?
Go to:

There are 919 identified and unidentified users. To see the list of identified users, Click here.

Sponsored links
  • Ask the community now
  • Publish
Ad
They won a badge
Join us in greeting them