• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
remrem Batch OS Detection routinesrem by Matt Borcherding, matt@borchtech.comremrem last updated 11-18-09rem updates posted on borchtech.blogspot.comremremrem Make sure temp directory exists - need it below routines...rem If not (for whatever reason), create it!remif not exist "%temp%" md "%temp%" > nul:Determine_Operating_Systemremrem Determine the current Windows operating system.remrem Detection routines based on some from:rem http://www.amset.info/loginscripts/os-id.aspremrem But fixed a few bugs, and added support forrem Vista/2008/7/2008R2 detection, Server Core detection,rem and 32/64-bit detection.remset opsys=win9xset bits=32set servercore=falseif %os%==Windows_NT goto NT_ver_checkremrem If the %OS% environmental variable doesn't exist, then the OSrem must be Win9x or Win ME.remgoto 64_bit_check:NT_ver_checkver > "%temp%\osver.txt"if exist "%windir%\system32\reg.exe" reg.exe query"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName >> "%temp%\osver.txt"remrem Vista and Server 2008 show identically in VER.EXErem Windows 7 and Server 2008 R2 also show identicallyrem in VER.EXE. So we need to check a registry keyrem instead to tell the difference.remrem Reg.exe doesn't get installed on NT or 2k, but VER.EXErem sufficies to identify those.
 
remrem If we can't detect the OS version, we assume NT4rem (lowest denominator).remrremrem Also now later detects Server Core (since a lot ofrem installs and such will fail or are unnecessary onrem Server Core).remrset opsys=nt4sfindstr "4.0" "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=win2k & goto 64_bit_checkifindstr "5.00." "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=win2k & goto 64_bit_checkifindstr "5.1." "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=winxp & goto 64_bit_checkifindstr "5.2." "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=win2003 & goto 64_bit_checkifindstr "Vista" "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=vista & goto 64_bit_checkifindstr "Server (R) 2008" "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=win2008 & set servercore=maybe & gotoserver_core_checksfindstr "Server 2008 R2" "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=win2008r2 & set servercore=maybe & gotoserver_core_checksfindstr "Windows 7" "%temp%\osver.txt" > nulif not errorlevel 1 set opsys=win7igoto 64_bit_checkgg:Server_Core_Checkremrem Server 2008 R2 added a registry key to check ifrem you are using Server Core:rem HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Installationtyperemrem But this doesn't work on straight 2008, which canrem also have Server Core installations. So just checkrem for explorer.exe. This doesn't exist in Server Corerem installs and is a lot simpler to check.remrif not exist "%windir%\explorer.exe" set servercore=trueii:64_bit_check
 
remrem Check if on a 64-bit OS. Setup program files directoryrem accordingly.remrem Win9x/Me only existed as 32-bit, so can skip for that OS.remrem Setup an environmental variable %pf% that refers to therem 32-bit Program Files directory on both 32-bit and 64-bitrem OSes. Using %pf% instead of %programfiles% thus makesrem most routines involving 32-bit software (still the vastrem majority of software) the same for both 32-bit and 64-bitrem OSes.remrem Since the environmental variable %programfiles(x86)% shouldrem only exist on 64-bit systems, we use that as our check.remrem We could also check against %PROCESSOR_ARCHITECTURE%rem instead (which reports either x86 or AMD64).remrem And my %bits% variable largely replicates %PROCESSOR_ARCHITECTURE%.rem But I'd *MUCH* rather just use the short and sweet %BITS%.remrrr set pf=%programfiles%if %opsys%==win9x goto 64_bit_checkedif defined programfiles(x86) set pf=%programfiles(x86)% & set bits=64i:64_bit_checked::remrem Cleanup after operating system checks...remrif exist "%temp%\osver.txt" del "%temp%\osver.txt" > nuliremrem Now branch out to do routines specific to the currentrem operating system. After that, run any routines appropriaterem for the current OS or older (e.g. if current OS is XP, runrem routines appropriate for XP, 2k, and NT.)remrgoto %opsys%gg:win9xremrem Win9x routines here...remgoto endgg:nt4remrem Windows NT routines here...
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...