Jaime Soriano
Jaime Soriano

Reputation: 7359

How to compare windows versions in a batch script?

I'm used to script programming in Linux, but now I need to do a very simple script for windows that does something depending on the version of the operative system.

I have seen that the ver command returns the version, but I don't know how to compare the output of this command with a string.

In pseudo-code I only need that:

version = system('ver')
if version > WINDOWS_XP_VERSION then
    do_something();
end if

or well, by now it'd also be ok enough to do:

version = system('ver')
if version in [WINDOWS_VISTA_VERSION, WINDOWS_7_VERSION] then
    do_something();
end if

My main concern is how to compare the output of a command.

Upvotes: 1

Views: 5441

Answers (2)

Joey
Joey

Reputation: 354466

set Version=
for /f "skip=1" %%v in ('wmic os get version') do if not defined Version set Version=%%v
for /f "delims=. tokens=1-3" %%a in ("%Version%") do (
  set Version.Major=%%a
  set Version.Minor=%%b
  set Version.Build=%%c
)

set GTR_XP=
if %Version.Major%==5 if %Version.Minor% GTR 1 set GTR_XP=1
if %Version.Major% GTR 5 set GTR_XP=1

if defined GTR_XP (
  ...
)

Upvotes: 2

publicRavi
publicRavi

Reputation: 2763

::dirty
for /f "tokens=9 delims=Version " %v in ('ver') do set version=%v
for /f "tokens=1 delims=]" %v in ('echo %version%') do set version=%v
if %version% geq 5.1.2600 ( echo yes )

If you are putting above in a batch file, please add an additional % before %v. I based this on Wikipedia article.

Upvotes: 0

Related Questions