Вы находитесь на странице: 1из 5

rem

rem Batch OS Detection routines


rem by Matt Borcherding, matt@borchtech.com
rem
rem last updated 11-18-09
rem updates posted on borchtech.blogspot.com
rem

rem
rem Make sure temp directory exists - need it below routines...
rem If not (for whatever reason), create it!
rem

if not exist "%temp%" md "%temp%" > nul

:Determine_Operating_System
rem
rem Determine the current Windows operating system.
rem
rem Detection routines based on some from:
rem http://www.amset.info/loginscripts/os-id.asp
rem
rem But fixed a few bugs, and added support for
rem Vista/2008/7/2008R2 detection, Server Core detection,
rem and 32/64-bit detection.
rem

set opsys=win9x
set bits=32
set servercore=false

if %os%==Windows_NT goto NT_ver_check

rem
rem If the %OS% environmental variable doesn't exist, then the OS
rem must be Win9x or Win ME.
rem

goto 64_bit_check

:NT_ver_check
ver > "%temp%\osver.txt"

if exist "%windir%\system32\reg.exe" reg.exe query


"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName >> "%temp
%\osver.txt"

rem
rem Vista and Server 2008 show identically in VER.EXE
rem Windows 7 and Server 2008 R2 also show identically
rem in VER.EXE. So we need to check a registry key
rem instead to tell the difference.
rem
rem Reg.exe doesn't get installed on NT or 2k, but VER.EXE
rem sufficies to identify those.
rem
rem If we can't detect the OS version, we assume NT4
rem (lowest denominator).
rem

rem
rem Also now later detects Server Core (since a lot of
rem installs and such will fail or are unnecessary on
rem Server Core).
rem

set opsys=nt4

findstr "4.0" "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=win2k & goto 64_bit_check

findstr "5.00." "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=win2k & goto 64_bit_check

findstr "5.1." "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=winxp & goto 64_bit_check

findstr "5.2." "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=win2003 & goto 64_bit_check

findstr "Vista" "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=vista & goto 64_bit_check

findstr "Server (R) 2008" "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=win2008 & set servercore=maybe & goto
server_core_check

findstr "Server 2008 R2" "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=win2008r2 & set servercore=maybe & goto
server_core_check

findstr "Windows 7" "%temp%\osver.txt" > nul


if not errorlevel 1 set opsys=win7

goto 64_bit_check

:Server_Core_Check
rem
rem Server 2008 R2 added a registry key to check if
rem you are using Server Core:
rem HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Installationtype
rem
rem But this doesn't work on straight 2008, which can
rem also have Server Core installations. So just check
rem for explorer.exe. This doesn't exist in Server Core
rem installs and is a lot simpler to check.
rem

if not exist "%windir%\explorer.exe" set servercore=true

:64_bit_check
rem
rem Check if on a 64-bit OS. Setup program files directory
rem accordingly.
rem
rem Win9x/Me only existed as 32-bit, so can skip for that OS.
rem
rem Setup an environmental variable %pf% that refers to the
rem 32-bit Program Files directory on both 32-bit and 64-bit
rem OSes. Using %pf% instead of %programfiles% thus makes
rem most routines involving 32-bit software (still the vast
rem majority of software) the same for both 32-bit and 64-bit
rem OSes.
rem
rem Since the environmental variable %programfiles(x86)% should
rem only exist on 64-bit systems, we use that as our check.
rem
rem We could also check against %PROCESSOR_ARCHITECTURE%
rem instead (which reports either x86 or AMD64).
rem
rem And my %bits% variable largely replicates %PROCESSOR_ARCHITECTURE%.
rem But I'd *MUCH* rather just use the short and sweet %BITS%.
rem

set pf=%programfiles%
if %opsys%==win9x goto 64_bit_checked
if defined programfiles(x86) set pf=%programfiles(x86)% & set bits=64

:64_bit_checked

rem
rem Cleanup after operating system checks...
rem

if exist "%temp%\osver.txt" del "%temp%\osver.txt" > nul

rem
rem Now branch out to do routines specific to the current
rem operating system. After that, run any routines appropriate
rem for the current OS or older (e.g. if current OS is XP, run
rem routines appropriate for XP, 2k, and NT.)
rem

goto %opsys%

:win9x
rem
rem Win9x routines here...
rem
goto end

:nt4
rem
rem Windows NT routines here...
rem

goto NT_or_greater

:win2k
rem
rem Win2k routines here...
rem

goto 2k_or_greater

:winxp
rem
rem WinXP routines here...
rem

goto WinXP_or_greater

:win2003
rem
rem Server 2003 routines here...
rem
rem

goto 2003_or_greater

:vista
:2008
rem
rem Vista/Server 2008 routines here...
rem

goto Vista_or_greater

:win7
:win2008r2
rem
rem Windows 7 / Server 2008 R2 routines here...
rem

:Vista_or_greater
rem
rem Any routines here appropriate for systems on Vista or newer
rem (Vista, Server 2008, 7, Server 2008 R2)
rem
:2003_or_greater
rem
rem Any routines here appropriate for systems on Server 2003 or newer
rem (Server 2003, Vista, Server 2008, 7, Server 2008 R2)
rem

:XP_or_greater
rem
rem Any routines here for systems on WinXP or newer
rem (XP, Server 2003, Vista, Server 2008, 7, Server 2008 R2)
rem

:2k_or_greater
rem
rem Any routines here appropriate for systems on Win2k or newer
rem (2k, XP, Server 2003, Vista, Server 2008, 7, Server 2008 R2)
rem

:NT_or_greater
rem
rem Any routines here appropriate for systems on NT or newer
rem (Anything but Win9x/ME)
rem

:end

Вам также может понравиться