• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2873
  • Last Modified:

Check OS version through batch file

Looking for a batch file to determine OS and branch accordingly.

EX:

If OS = Windows 95 goto w95
if OS = Windows XP goto wxp
etc.

This should have been placed in Windows Batch.  
0
pony10us
Asked:
pony10us
  • 4
  • 3
  • 3
  • +3
4 Solutions
 
d_williaCommented:
The built-in VER command will give you the Windows version.  For example, when I type it in a command window, I get:

Microsoft Windows [Version 6.1.7600]

Hope this helps.
0
 
renov8rCommented:
The "VER" command returns the approbate information to use in a batch script.

You will probably want to do some parsing. Here is a common construct that should get you started:

FOR /F "tokens= 4*" %%G IN ('ver') DO SET _version=%%G

echo %_version%
0
 
QlemoDeveloperCommented:
... this has the disadvantage you have to "decode" the version string. But I do not know of any more easy method you can use in a batch file. Usually, you process that info like this:

for /F "tokens=4-5 delims=[.] " %%A in ('ver') do set version=%%A.%%B
if "%version%" == "6.1" echo W7 or W2008R2
if "%version%" == "6.0" REM Vista or W2008
if "%version%" == "5.2" REM W2003 or XP x64
if "%version%" == "5.1" REM XP
if "%version%" == "5.0" REM W2000

But that does not work for W9x, because they do not know the FOR /F syntax.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
d_williaCommented:
Or, since you are only trying to determine the difference between win95 and winxp, you could use the existing environment variable "OS" like this

goto %OS%

Windows 200 and up is set to "Windows_NT".  You will have to check to see what Windows 95 has.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Hey Poney, I can't promise this is the newest iteration of my script (in fact I'm quite sure it isn't because the part where it finds the architecture of the system is far over-done from my newer method)  but it's the one I was able to find most recently

(I tinker with this off and on because it's part of many of my scripts, as you can see the script's cchecking for version is succh an old feature of my batch scripting that it actually occurs in the upper portion instead of being a sub-function to be called one time. ^^  that is kinda old-school for me ^^  but I just haven't concentrated on it in one fell swoop to make it more efficient and turn it into a true sub function yet ^^

::------------------------------------------------------------------------------------------------::
:: Script: RoboCopy_Backup-SharePoint_Any-Drive.bat                                               ::
:: Version: 2.0                                                                                   ::
:: Copyright: Ben Personick                                                                       ::
:: Date: 2010-07-21                                                                               ::
::                                                                                                ::
::          +                                                                                     ::
:: Desc: Copies Sharepoint documents to a backup location on the HD it's run from				  ::
::                                                                                                ::
::------------------------------------------------------------------------------------------------::
:Begin
  ECHO Off
  SET ScriptName=RoboCopy_Backup-SharePoint
  ECHO ---- Begining %ScriptName%... ----

::------------------------------------------------------------------------------------------------::
:: This section sets up default variable settings and sets the logging directory and file name.   ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
	ECHO off
	CALL :Start-Set-Date
	SET Preamble=%ScriptName% --
	
	::--------------------------------------------------------------------------------------------::
	:: This Section Sets up logging variables.													  ::
	::--------------------------------------------------------------------------------------------::
	SET IDir=%DrvDst%\Admin\%ScriptName%\Logs
	SET ILog=%ScriptName%_%TDate%_%TTime%.Log
	SET IDL=%IDir%\%ILog%
	
	::--------------------------------------------------------------------------------------------::
	:: This Section Creates Directories for Logging and other files								  ::
	::--------------------------------------------------------------------------------------------::
	:Start-Create-Directories
		For /F %%D IN ("%IDir%") DO CALL :Start-DIR-Loop %%D
		GOTO End-DIR-Loop
		:Start-DIR-Loop
			SET TDir=%1
			ECHO %Preamble% Checking For %TDir%
			IF EXIST %TDir% CALL :Start-Echo "%TDir% -- Found!"
			IF EXIST %TDir% GOTO :EOF
			ECHO %Preamble% Creating %TDir%
			MKDir %TDir%
			CALL :Start-Echo "Created %TDir%"
			GOTO :EOF
		:End-DIR-Loop
		CALL :Start-Echo "Dir-Loop Completed"
	:End-Create-Directories
	
	::--------------------------------------------------------------------------------------------::
	:: This Section Sets Default Values for match variables, don't change except for testing.	  ::
	::--------------------------------------------------------------------------------------------::
	SET OSVer=NOMATCH
	Set vTemp=NOMATCH
	SET OSSP=NOMATCH
	SET OSbit=NOMATCH
	SET OSSPExit=NOEXIT
	SET RoBoVista=NO
	SET SkipLoop=NO

:End-Prep

:Start-Begin-Log
	CALL :Start-Set-Date
	CALL :Start-Echo " "
	CALL :Start-Echo "------------------------------------------------------"
	CALL :Start-Echo "BEGIN %ScriptName% Script on %TDate% AT %TTime%"
	CALL :Start-Echo "------------------------------------------------------"
	CALL :Start-Echo " "
:End-Begin-Log



::------------------------------------------------------------------------------------------------::
:: This section finds the version of the operating system for use later in the script             ::
::------------------------------------------------------------------------------------------------::
:Start-Find-OS-Info

	:Start-FIND-Ver
		FOR /F "Tokens=4 Delims=[] " %%V IN ('Ver ^| find "Version"') DO CALL :Start-Ver-Match "%%V"
		GOTO End-Ver-Match
		:Start-Ver-Match
			SET Ver=%~1
			Call :Start-Echo Windows "Ver" is "%Ver%"
			IF /I %Ver:~0,1% Gtr 3 IF /I "%Ver:~3,1%" NEQ "." SET OSVer=Win9x
			IF /I "%Ver%"=="3.10.528" SET OSVer=NT3.1
			IF /I "%Ver%"=="3.50.807" SET OSVer=NT3.5
			IF /I "%Ver%"=="3.51.1057" SET OSVer=NT3.51
			IF /I "%Ver%"=="4.0.1381" SET OSVer=NT4.0
			IF /I "%Ver%"=="5.0.2195" SET OSVer=2K
			IF /I "%Ver%"=="5.1.2600" SET OSVer=XP
			IF /I "%Ver%"=="5.2.3790" SET OSVer=2K3
			IF /I "%Ver%"=="6.0.6000" SET "OSVer=Vista" & SET "RoBoVista=GO"
			IF /I "%Ver%"=="6.0.6001" SET "OSVer=Vista" & SET "RoBoVista=GO" & SET OSSP=SP1
			IF /I "%Ver%"=="6.0.6002" SET "OSVer=Vista" & SET "RoBoVista=GO" & SET OSSP=SP2
			IF /I "%Ver%"=="6.1.7600" SET "OSVer=7" & SET "RoBoVista=GO"
			
			GOTO :EOF
		:End-Ver-Match
		CALL :Start-Echo "End-Ver-Match"
		
		IF "%RoBoVista%" NEQ "GO" GOTO 2K8?-end
		:2k8?-start
			CALL :Start-Echo "2k8?-start"
			START /WAIT REGEDIT.EXE /E "%Temp%.\%%~n0.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" 
			  FOR /F "tokens=2 delims==" %%A IN ('TYPE "%Temp%.\%%~n0.dat" ^| FIND /I "ProductName" ^| Find /I "2008"') DO SET OSVer=2K8
			
		:2k8?-end

	:End-Find-Ver
	 
	
	:Start-FIND-SP
		CALL :Start-Echo "Service pack: %OSSP%"
		IF /I %Ver:~0,1% GEQ 5 IF /I "%Ver%" NEQ "5.0.2195" FOR /F "Tokens=5" %%R IN ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CSDVersion ^| FIND /I "CSDVersion"') Do SET OSSP=SP%%R
		IF /I "%OSSP%" NEQ "NOMATCH" GOTO End-FIND-SP
		START /WAIT REGEDIT.EXE /E "%Temp%.\%%~n0.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" 
		FOR /F "tokens=4 delims== " %%A IN ('TYPE "%Temp%.\%%~n0.dat" ^| FIND /I "CSDVersion"') DO SET OSSP=SP%%A
		SET OSSP=%OSSP:"=%
		DEL "%Temp%.\%%~n0.dat" 
		CALL :Start-Echo "Service Pack: %OSSP%"
		IF /I "%OSSP%"=="SP0" GOTO End
		FOR %%S IN (1, 2, 3, 4, 5, 6) DO call :Start-SP-Loop %%S
		CALL :Start-Echo "End 2nd for"
		GOTO End-SP-Loop
		:Start-SP-Loop
			If %OSSPExit% == EXIT GOTO :EOF
			SET TSP=%1
			CALL :Start-Echo "TSP = %TSP%"
			ECHO %OSSP% | FIND "%TSP%"
			IF ERRORLEVEL 1 GOTO :EOF
			SET OSSP=SP%TSP%
			SET OSSPExit=EXIT
			GOTO :EOF
			
		:End-SP-Loop
		CALL :Start-Echo "End-SP-Loop"
		
	:End-FIND-SP
	CALL :Start-Echo "End Find SP"

::------------------------------------------------------------------------------------------------::
:: This section finds Whether the Operating system is 32bit or 64bit for use later in the script. ::
::------------------------------------------------------------------------------------------------::
	:Start-Find-OS-Info-Architechture

		:Start-Find-bit-Level

			FOR %%V IN (86, 64) DO call :Start-bit-Loop %%V
			GOTO End-bit-Loop
			:Start-bit-Loop
				If %OSbit% NEQ NOMATCH GOTO :EOF
				SET Tbit=%1
				CALL :Start-Echo "Tbit = %Tbit%"
				ECHO %PROCESSOR_ARCHITECTURE% | FIND "%Tbit%"
				IF ERRORLEVEL 1 GOTO :EOF
				SET OSbit=%Tbit%
				If %Tbit% == 86 SET OSbit=32
				GOTO :EOF
				
			:End-bit-Loop
			CALL :Start-Echo "End-bit-Loop"
			
		:End-Find-bit-Level

	:End-Find-OS-Info-Architechture
	
::------------------------------------------------------------------------------------------------::
:: This section is intended to determin if the OS is Desktop or Server for differing installs.    ::
::------------------------------------------------------------------------------------------------::
	:Start-Set-Desktop-OS-Runs
		SET DTOSRun=NO
		CALL :Start-Echo "Checking if Desktop OS"
		CALL :Start-Echo "DTOSRun = %DTOSRun%, OSVer = %OSVer%, Ver = %Ver%"
		CALL :Start-Echo "Set DTOSRun to GO Only if OSVer is a desktop OS"
		IF /i %OSVer% == XP SET DTOSRun=GO
		IF /i %OSVer% == Vista SET DTOSRun=GO
		IF /i %OSVer% == 7 SET DTOSRun=GO
		
	:End-Set-Desktop-OS-Runs
	
	CALL :Start-Echo "OS Version = %OSVer%"
	CALL :Start-Echo "OS SP Version = %OSSP%"
	CALL :Start-Echo "OS Architechture = %OSbit% bit"
	CALL :Start-Echo "OS Desktop? (NO or GO) = %DTOSRun%"
	
:End-Find-OS-Info




:Start-Main



:End-Main
GOTO :End


GOTO End-Subs
GOTO :End
:Start-Subs
	GOTO :End-Set-Date
	:Start-Set-Date
		SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
		SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
		SET TTime=%TTime: =0%
		GOTO :EOF
	:End-Set-Date
	GOTO End-Echo
	:Start-Echo
		Set TEcho=%~1
		ECHO %Preamble% %TEcho% --
		ECHO %Preamble% %TEcho% -- >> %IDL%
		GOTO :EOF
	:End-Echo
	
:End-Subs
GOTO :End

:End
	CALL :Start-Set-Date
	CALL :Start-Echo "!!--!! %ScriptName% Exiting ON %TDate% AT %TTime% !!--!!
	GOTO :EOF

Open in new window

0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Yeahp, agreed, I very much thought we might even get something better, but thanks for the points I totally agree with the split =)
0
 
JofnnCommented:
You can use

VER|FIND "Windows 2000" >NUL
If errorlevel 0 goto WhateverYouWantForWin2k
VER_FIND "Windows XP" >NUL
If errorlevel 0 goto WhateverYouWantForWinXP

etc etc
0
 
JofnnCommented:
the second one was supposed to use "VER|FIND" rather than "VER_FIND"
0
 
QlemoDeveloperCommented:
Jofnn,
No you can't. VER is always resulting in "Microsoft Windows [version]". XP or 2000 or the like are not even mentioned.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Jofnn, Ver will not include the "Windows 2000" "Windows XP" (etc.) tags it will only ever include a version number.

  Some version numbers are even the same even though you or I might want them to be considered differently.

  Check my posted solution to see a full list of version numbers from Windows NT 3.1 to Windows Server 2008 R2
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
lol Qlemo beet me to it ^^  tnx Qlemo ^^
0
 
JofnnCommented:
Qlemo/QCubed - the "VER" command does actually return the name as well (in Win2K Pro & WinXP Pro at least) in the following format:

Microsoft Windows xx [Version x.x.xxxx]

I use it in a few of my scripts to determine the OS so that it controls which menu the user is presented with... so I know for sure that it works... Having said that - this may be something in the PRO OS's??
0
 
QlemoDeveloperCommented:
You are correct, but that stopped with W2003. Since then, all "versions" are "Microsoft Windows".
0
 
pony10usAuthor Commented:
This has turned into a lively discussion now.  I have increased the overall point value to 500 but am hesitant to close it again just yet.

Jofnn:  Qlemo is correct. The results of running VER on a windows 2003 server:  
Microsoft Windows [Version 5.2.3790]

vs. running on a windows XP Pro workstation:
Microsoft Windows XP [Version 5.1.2600]

0
 
pony10usAuthor Commented:
It seems the discussion has died down but I do appreciate everyone's input. Thank you again QCubed for the code and everyone else for confirming what QCubed and I were thinking. Since the question was for QCubed to post his code I awarded him 200 of the 500 and split the remaining 300 (100 each) between the others.

Thank you again everyone.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now