Friday, September 2, 2011

How to set the output of a command-line program to a variable in a Windows batch file

Example 1: Set full Windows version string to a variable

@echo off
for /f "delims=" %%a in ('ver') do set version=%%a
echo %version%

Output:

Microsoft Windows [Version 6.1.7600]

Example 2: Set Windows version to a variable

@echo off
for /f "tokens=4" %%a in ('ver') do set version=%%a
for /f "delims=] tokens=1" %%a in ("%version%") do set version=%%a
echo %version%

Output:

6.1.7600

More information: For

No comments:

Post a Comment