Solved

Check OS version through batch file

Posted on 2010-09-13
16
2,481 Views
Last Modified: 2012-06-27
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
Comment
Question by:pony10us
  • 4
  • 3
  • 3
  • +3
16 Comments
 
LVL 3

Assisted Solution

by:d_willia
d_willia earned 100 total points
ID: 33665819
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
 
LVL 2

Assisted Solution

by:renov8r
renov8r earned 100 total points
ID: 33665901
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
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 33665945
... 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
 
LVL 3

Expert Comment

by:d_willia
ID: 33666045
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
 
LVL 11

Accepted Solution

by:
Ben Personick earned 200 total points
ID: 33667070
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
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33667595
Yeahp, agreed, I very much thought we might even get something better, but thanks for the points I totally agree with the split =)
0
 
LVL 3

Expert Comment

by:Jofnn
ID: 33669881
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:Jofnn
ID: 33669886
the second one was supposed to use "VER|FIND" rather than "VER_FIND"
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 33669903
Jofnn,
No you can't. VER is always resulting in "Microsoft Windows [version]". XP or 2000 or the like are not even mentioned.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33669921
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
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33669923
lol Qlemo beet me to it ^^  tnx Qlemo ^^
0
 
LVL 3

Expert Comment

by:Jofnn
ID: 33670002
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 33670685
You are correct, but that stopped with W2003. Since then, all "versions" are "Microsoft Windows".
0
 
LVL 26

Author Comment

by:pony10us
ID: 33676303
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
 
LVL 26

Author Closing Comment

by:pony10us
ID: 33695162
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now