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

wrybread

Distinguished
Feb 23, 2005
18
0
18,510
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.
 

pscowboy

Distinguished
Apr 24, 2002
1,129
0
19,290
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.
 

wrybread

Distinguished
Feb 23, 2005
18
0
18,510
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).
 

spikejones

Distinguished
Feb 14, 2009
1
0
18,510
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%

wrybread said:
I tried piping it, but can't figure out what the syntax would be.
there's your piped command

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

gary_baird

Distinguished
Jun 16, 2009
5
0
18,510
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%
 

eladkarako

Distinguished
Jul 21, 2009
1
0
18,510
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.

 

gary_baird

Distinguished
Jun 16, 2009
5
0
18,510
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.
 
G

Guest

Guest
eladkarako


That's really nicely presented thanks.

One additional facility is that if you want a pipe in the command simply preceed the pipe symbol with a "^".


'VER ^| more'

Thanks to spikejones for the latter - it was burried in the code and I thought it worth highlighting.

Is there a decent tutorial on the new windows batch stuff (newer than 1990:)?

I have programmed in everything from 6502/Z80/8086 assembler to C and perl however I find the fancy Windows batch stuff completely opaque. Maybe I'm just too old:(

 
G

Guest

Guest
Is there a decent tutorial on the new windows batch stuff (newer than 1990:)?

I have programmed in everything from 6502/Z80/8086 assembler to C and perl however I find the fancy Windows batch stuff completely opaque. Maybe I'm just too old:(
Not a real tutorial, but I've found the site http://ss64.com very helpful. It has an A-Z index of all windows CMD commands. Much like Microsoft's own information on MSDN or Technet (I forget which), but with some elaboration and added examples. It also contains a number of very useful pages on syntax.
Almost every time I'm trying to write a batch file, I visit that page to check some things.

Besides that site, just search Google for what you want to achieve. There's a lot of info on command line and batch file programming out there, but it's scattered around on blog postings and in forums and such.

I've picked up a lot by just trying things, seeing that my initial attempt didn't work, and then search the net for working examples.

And even something as simple as the output of the 'help' command, or 'help [command]' can be very useful sometimes.
 

outbackkid

Distinguished
Feb 6, 2012
1
0
18,510
Thanks spike, that is genius. I had to double the percentages to get it to work.

I'm using it to run a vbscript that generates a random number. vbscript seems to be better at generating different numbers when two separate processes request a random number at exactly the same time. :sol:
 

yinemibamya

Honorable
Mar 8, 2012
1
0
10,510
Hi all,

I have a problem like mentioned here. I hope you find it interesting to solve.

I need to read output lines on the command window and process those lines.
Let me give an example:

When I type "mycommand.bat", these lines are flowing on the command window:

Starting server
Checking license 1
Checking license 2
Checking license 3
Success.

This is the case if required license files are available.
On the other hand if licenses files are not available, these lines are flowing on the command window:

Starting server
Checking license 1
Checking license 2
Checking license 3
Failed to get all license files.

What I need to do is reading these lines and check them whether "Success" or "Failed" outputs.
If the output is "Failed" I'm supposed to wait for a while and re-execute "mycommand.bat" again.
So "mycommand.bat" will be able to get licenses automatically for a given period.
 

lapinchaman

Honorable
Aug 22, 2012
1
0
10,510
"What I need to do is reading these lines and check them whether "Success" or "Failed" outputs." :

i would use the qgrep for this:

mycommand.bat | qgrep -y "failed"

will isolate only the lines containing "failed" or "Failed" (the switch '-y' makes it case insensitive).
qgrep is the windoze implementation of the *NIX grep. it first came (i think it still does) in the Resource Kit, but you find it on the web.
 

datico

Honorable
Nov 15, 2012
1
0
10,510


[strike]Windows 2008 Server R2 SP1 the following works:
>set a=^|ver
>echo %a%
Microsoft Windows [Version 6.1.7601][/strike]

Edit: Nevermind! I'm off my rocker, it's just evaluating "ver" at the time of the echo, a is actually the string "|ver".
 

tuxine

Honorable
Dec 5, 2012
1
0
10,510


Have you tried to split the std-out and std-err? I solved a similar problem with following code:

mybatch.bat 2>> mybatch.bat.err >> mybatch.bat.out

The bachtfile "mybatch.bat" apppends ERR-Output to the "mybatch.bat.err" and normal output to the file to "mybatch.bat.out". (Double >> makes append, single > overwrites)

 

Lalithkiller

Honorable
Dec 12, 2012
1
0
10,510



If i This is in batch file. It does not get executed. What should I do to make it work. I want to copy files based upon Windows version. I am assigning this value to a variable and comparing it.