Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Created December 6, 2012 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidruhmann/4229476 to your computer and use it in GitHub Desktop.
Save davidruhmann/4229476 to your computer and use it in GitHub Desktop.
[Batch] Get and Verify the version of Windows
:: Get and Verify the version of Windows
:: by David Ruhmann
:: Hide Commands
@echo off
echo.
call :WindowsVersion xMajor xMinor
echo.%xMajor%
echo.%xMinor%
echo.
echo.Strict 6.1
call :VerifyVersion xResult 6 1 true
echo.%xResult%
echo.
echo.Not Strict
call :VerifyVersion
echo.%xResult%
echo.
pause
goto End
:End
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:VerifyVersion <xReturn> [xMajor] [xMinor] [xStrict]
:: Validates that the current version of Windows is valid for this script.
:::: Defaults to 5.0+ if the input is invalid or not specified.
:::: Uses global variables xLog and xSession upon error.
:: Returns true if the version is valid, else false.
:: Isolate Scope
setlocal
:: Setup
set xResult=false
set xStrict=%~4
set xMajor=%~2
set xMinor=%~3
:: Validate Values
:: Defaults: Windows versions 5.0+ aka Windows 2000+ and not version strict.
call :IsNumber xReturn %xMajor%
if /i not "%xReturn%"=="true" set "xMajor=5"
call :IsNumber xReturn %xMinor%
if /i not "%xReturn%"=="true" set "xMinor=0"
if /i "%xStrict%"=="true" set "xStrict=true"
:: Retrieve the Version
set "xWinMajor=0"
set "xWinMinor=0"
call :WindowsVersion xWinMajor xWinMinor
:: Check the Version
if /i "%xStrict%"=="true" if %xWinMajor% EQU %xMajor% if %xWinMinor% EQU %xMinor% set "xResult=true"
if /i not "%xStrict%"=="true" if %xWinMajor% GEQ %xMajor% if %xWinMinor% GEQ %xMinor% set "xResult=true"
endlocal & if not "%~1"=="" set "%~1=%xResult%"
goto :eof
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:WindowsVersion <xMajor> <xMinor>
:: Retrieve the Windows Version.
:: Isolate Scope
setlocal
:: Verify Version Command
ver >nul 2>&1
if ErrorLevel 0 goto GetWindowsVersion
call :Line xLine eWindowsVersion -2
call :Log "%xLog%" "ERROR: Unable to execute version command. %0 @ %xLine%." 1 "%xSession%"
goto :eof
:GetWindowsVersion
:: Retrieve and Parse Version
for /f "tokens=2,3 delims=[.]" %%a in ('ver') do (
set "xMajor=%%a"
set "xMinor=%%b"
)
set xMajor=%xMajor:Version =%
rem call :Log "%xLog%" "Windows: %xMajor%.%xMinor%" 1 "%xSession%"
:: Return Version
endlocal & if not "%~1"=="" set "%~1=%xMajor%" & if not "%~2"=="" set "%~2=%xMinor%"
goto :eof
:: by David Ruhmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment