Oct 13, 2013

Function: OsCheck

A function that will let your scripts recognize Windows version. Works globally, as you can see below. The OS identifier is kept in osid variable, so you needn't call it many times. Two another variables are flushed just after calling OsCheck. I have nothing more to say about this trivial code.
:: works globally, uses vars osid, os_str, os_str_  
@echo off
echo.
call:OsCheck
for %%A in (os_str os_str_) do (if defined %%A (set %%A=))
if "%osid%"=="0" (echo  Unknown OS, probably older than Windows 2000.)
if "%osid%"=="1" (echo  Windows 2000)
if "%osid%"=="2" (echo  Windows XP)
if "%osid%"=="3" (echo  Windows Server 2003)
if "%osid%"=="4" (echo  Windows Vista)
if "%osid%"=="5" (echo  Windows Server 2008)
if "%osid%"=="6" (echo  Windows 7)
if "%osid%"=="7" (echo  Windows 8)
if "%osid%"=="8" (echo  Unknown OS, probably newer than Windows 8.)
echo.
pause
goto:eof
:OsCheck
if not exist "%windir%\system32\reg.exe" (for /f "tokens=3" %%A in ('ver') do (if "%%A"=="2000" (set osid=1&exit/b0) else (set osid=0&exit/b0)))
for /f "tokens=3*" %%G in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "ProductName"') do (set os_str=%%G %%H)
set os_str_=%os_str:Windows 7=%
if not "%os_str%"=="%os_str_%" (set osid=6&exit/b0)
set os_str_=%os_str:Windows 8=%
if not "%os_str%"=="%os_str_%" (set osid=7&exit/b0)
set os_str_=%os_str:XP=%
if not "%os_str%"=="%os_str_%" (set osid=2&exit/b0)
set os_str_=%os_str:Vista=%
if not "%os_str%"=="%os_str_%" (set osid=4&exit/b0)
set os_str_=%os_str:2003=%
if not "%os_str%"=="%os_str_%" (set osid=3&exit/b0)
set os_str_=%os_str:2008=%
if not "%os_str%"=="%os_str_%" (set osid=5&exit/b0)
set os_str_=%os_str:2000=%
if not "%os_str%"=="%os_str_%" (set osid=1&exit/b0)
set osid=8
exit/b0

No comments:

Post a Comment