• Ask the community now
  • Publish
Ad

News

SimCity For Big Boys: Preparing For The Katrina Of Earthquakes

Feature -Have you ever wondered what could have been, if New Orleans and the federal government had been prepared for a devastating Hurricane such as Katrina? Read more

Nvidia fires away with new Geforce 8800 GPU

Nvidia today introduced its latest and greatest graphics chip, the Geforce 8800. Previously code-named G80, the new GPU offers more functionality and a significant rise in the sheer power of the GPU - including DirectX 10 support and a capable physics engine. Extra: Image Gallery Read more

CES 2006: Toshiba announces Qosmio notebook computer with HD DVD-ROM drive

Toshiba is showing off its newest Qosmio notebook computers at CES. The 17" notebook with Centrino Duo processor looks to be the first on the block with an HD DVD-ROM drive. Read more

Latest Reviews & Articles

Windows 7 Versus XP: Which Belongs On Your Netbook?

Windows 7 Versus XP: Which Belongs On Your Netbook?

Windows Vista was never really an option for netbook users, but the release candidate of Windows 7 piqued our interest. Can Microsoft finally retire Windows XP in the netbook space? Even now with RC1 available, the answer seems tied to driver development. Read more

A Lesson In Backup: Taking Care Of Your Data

A Lesson In Backup: Taking Care Of Your Data

If you aren't regularly backing up your data, now's a good time to start. In this piece we cover some of the basic backup strategies, a handful of available tools, and backup performance over USB 2.0 using Samsung's newest Story Station external drive. Read more

Killer Xeno Pro: Do You Really Need A Gaming Network Card?

Killer Xeno Pro: Do You Really Need A Gaming Network Card?

Bigfoot Networks has updated its gaming Network Processing Units to support PCI Express connectivity. The new Killer Xeno Pro has a lot of new features, but can it really deliver higher performance for a gamer? We test the new card in order to find out. Read more

ADVERTORIAL Microsoft BPOS: Taking Action

ADVERTORIAL Microsoft BPOS: Taking Action

Moving to Microsoft Business Productivity Online Suite quickly and smoothly may require the help of a qualified advisor. That's an opportunity for potential BPOS customers and those who want to become BPOS advisors. Read more

All the Reviews & Articles
Tom's Hardware > Forum > Windows XP > Windows XP General Discussion > Windows batch file: set output of program to a variable?

Windows batch file: set output of program to a variable?

Tom's Hardware: Over 1.4 million members in 6 different countries available to answer all your high-tech questions. Sign up now! Its free!
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
Tom's Hardware > Forum > Windows XP > Windows XP General Discussion > Windows batch file: set output of program to a variable?
Go to:

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

Sponsored links