How can one determine what version of Windows and/or cmd.exe a batch file is running on?
如何确定Windows和/或cmd的版本。exe正在运行一个批处理文件吗?
There is no cmd /version
that I've been able to find and the results of SET in a command prompt session don't give anything obviously unique (between XP and Win7 anyway).
我没有找到cmd /版本,而且命令提示会话中的SET结果没有给出任何明显的惟一性(无论如何,在XP和Win7之间)。
7 个解决方案
#1
7
The version of cmd.exe
should actually be pretty irrelevant, unless you try to use features that didn't exist before (in command.com
for example). There is the pseudovariable
cmd的版本。exe实际上应该是非常不相关的,除非您尝试使用以前不存在的特性(例如command.com)。有准变星
%cmdextversion%
which holds the version of the command extensions which has been 2 for ages (at least back to NT 4, iirc).
它保存了命令扩展的版本,这个版本已经有2年的历史了(至少可以追溯到NT 4, iirc)。
But, back to the point: Running ver
and parsing the version string might be your best bet:
但是,回到正题:运行ver并解析版本字符串可能是您最好的选择:
for /f "tokens=2 delims=[]" %%x in ('ver') do set WINVER=%%x
set WINVER=%WINVER:Version =%
#2
15
you can use the "systeminfo" @ cmd.exe
您可以使用“systeminfo”@ cmd.exe
C:\Users\Tagon8>systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name: Microsoft Windows 8 Release Preview
OS Version: 6.2.8400 N/A Build 8400
#3
7
I found a shorter way using ver as well:
我还发现了一种使用ver的更短的方法:
Could be even shorter:
可以更短:
ver | find "5.1" >nul && goto ver_winxp
#4
3
I found a shorter way using ver
as well:
我还发现了一种使用ver的更短的方法:
...
ver | find "5.1"
if %ERRORLEVEL% == 0 goto ver_winxp
...
This will find XP, replace the string with your wanted versions
这将找到XP,用您想要的版本替换字符串
#5
2
Type "ver" at a command prompt.
在命令提示符下输入“ver”。
Next time around, since this isn't really programming related but server or user related, you might try serverfault.com or superuser.com.
下次,由于这与编程无关,而是与服务器或用户有关,您可以尝试serverfault.com或superuser.com。
#6
1
Maybe someone will need the following to determine the SKU (Win7). I'm using some of this script to pick the right OS and XML during sysprep. Hope it helps!
也许有人需要以下内容来确定SKU (Win7)。在sysprep过程中,我使用其中的一些脚本选择正确的OS和XML。希望它可以帮助!
@echo off
set ver=Unknown
systeminfo > C:\sysinfo
findstr /e /c:"Enterprise " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Enterprise
findstr /e /c:"Ultimate " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Ultimate
findstr /e /c:"Professional " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Professional
findstr /e /c:"Home Premium " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Home Premium
findstr /e /c:"Home Basic " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Home Basic
del /f /q C:\SP\sysinfo 1>nul 2>nul
Echo Windows 7 %ver%
pause
exit
#7
0
The internal command ver
reports windows version number (which could have been learned by typing help
at the command prompt).
内部命令ver报告windows版本号(可以通过在命令提示符中输入帮助来学习)。
There is a dynamic variable %CMDEXTVERSION%
, but it hasn't progressed in several releases so it's only useful for delineating between Windows NT and Windows 2000 and newer. (Thanks @Joey, here.)
有一个动态变量%CMDEXTVERSION%,但是它在几个版本中都没有进展,所以它只用于描述Windows NT和Windows 2000以及更新版本之间的关系。(谢谢@Joey这里。)
Here's a batch to parse the output of ver for XP and newer, courtesy of Simon Sheppard:
下面是一个解析XP和更新版本的ver输出的批:
@echo off
Setlocal
:: Get windows Version numbers
For /f "tokens=2 delims=[]" %%G in ('ver') Do (set _version=%%G)
For /f "tokens=2,3,4 delims=. " %%G in ('echo %_version%') Do (set _major=%%G& set _minor=%%H& set _build=%%I)
Echo Major version: %_major% Minor Version: %_minor%.%_build%
if "%_major%"=="5" goto sub5
if "%_major%"=="6" goto sub6
Echo unsupported version
goto:eof
:sub5
::Winxp or 2003
if "%_minor%"=="2" goto sub_2003
Echo Windows XP [%PROCESSOR_ARCHITECTURE%]
goto:eof
:sub_2003
Echo Windows 2003 or xp 64 bit [%PROCESSOR_ARCHITECTURE%]
goto:eof
:sub6
if "%_minor%"=="1" goto sub7
Echo Windows Vista or Windows 2008 [%PROCESSOR_ARCHITECTURE%]
goto:eof
:sub7
Echo Windows 7 or Windows 2008 R2 [%PROCESSOR_ARCHITECTURE%]
goto:eof
And here's my own fairly complete, largely academic, kick at the can which returns the parsed version number as environment variables:
这是我自己的一个相当完整的,主要是学术性的,可以返回解析后的版本号作为环境变量:
@echo off
setlocal
:: from http://ss64.org/viewtopic.php?pid=3136#p3136
::==================================
::variables
if %PROCESSOR_ARCHITECTURE%==x86 set pro_arch=32 Bit (x86)
if %PROCESSOR_ARCHITECTURE%==AMD64 set pro_arch=64 Bit (AMD64)
if %PROCESSOR_ARCHITECTURE%==IA64 set pro_arch=Itanium 64 Bit (IA64)
:Main
call :clean
for /f "tokens=2 delims=[]" %%x in ('ver') do set cmdver=%%x
set cmdver=%cmdver:Version =%
call :parse_cmdver
call :ver%cmdver%
call :Report
goto :End
:clean
:: Ensure we don't inherit values from previous runs
set _verCmd=
set _verMajor=
set _verMinor=
set _verBuild=
set _verWin=
goto :eof
:Parse_cmdver
:: Turn "5.1.2306" string into actionable variables
for /f "tokens=1,2,3* delims=." %%g in ("%cmdver%") do (
set major=%%g
set minor=%%h
set build=%%i
)
goto :eof
:Report
echo.
echo. CMD version is %cmdver%
echo. which probably means %longver% %pro_arch%
echo.
goto :eof
:Report2
echo. The numbers are stored in the following variables:
echo.
set _ver
goto :eof
::Table of version numbers built from
:: http://en.wikipedia.org/wiki/Microsoft_Windows#Timeline_of_releases
:ver1.01
set longver=Windows 1.01
set shortver=Win101
goto :eof
:ver2.03
set longver=Windows 2.03
set shortver=Win203
goto :eof
:ver2.10
set longver=Windows 2.10
set shortver=Win21
goto :eof
:ver2.11
set longver=Windows 2.11
set shortver=Win211
goto :eof
:ver3.0
set longver=Windows 3.0
set shortver=Win3
goto :eof
:ver3.1
set longver=Windows 3.1, Windows For Workgroups 3.1, or Windows NT 3.1
set shortver=Win31/WFW31/WinNT31
goto :eof
:ver3.11
set longver=Windows For Workgroups 3.11
set shortver=WFW311
goto :eof
:ver3.2
set longver=Windows 3.2 (released in Simplified Chinese only)
set shortver=Win32ch
goto :eof
:ver3.5
set longver=Windows NT 3.5
set shortver=WinNT35
goto :eof
:ver3.51
set longver=Windows NT 3.51
set shortver=WinNT351
goto :eof
:ver4.0.950
set longver=Windows 95
set shortver=Win95
goto :eof
:ver4.0.1381
set longver=Windows NT 4.0
set shortver=WinNT4
goto :eof
:ver4.90.3000
set longver=Windows Me
set shortver=WinMe
goto :eof
:ver4.10.1998
set longver=Windows 98
set shortver=Win98
goto :eof
:ver4.10.2222
set longver=Windows 98 SE
set shortver=Win98SE
goto :eof
:ver5.0.2195
set longver=Windows 2000
set shortver=Win2K
goto :eof
:ver5.1.2600
set longver=Windows XP or Windows Fundamentals for Legacy PCs
set shortver=WinXP/WinFun
goto :eof
:ver5.2.3790
set longver=Windows XP, Windows XP Pro or Windows Server 2003
set shortver=WinXP/WinXP-Pro/Server2003
goto :eof
:ver5.2.4500
set longver=Windows Home Server
set shortver=WinHomeServer
goto :eof
:ver6.0.6002
set longver=Windows Vista or Windows Server 2008
set shortver=Vista/Server2008
goto :eof
:ver6.1.7600
set longver=Windows 7 or Windows Server 2008 R2
set shortver=Win7/Server2008R2
goto :eof
:End
:: return version to calling shell/script,
:: see http://ss64.com/nt/syntax-functions.html
endlocal & set _verCmd=%cmdver% & set _verMajor=%major% & set _verMinor=%minor% & set _verBuild=%build% & set _verWin=%shortver%
call :Report2 :: comment this line out to suppress extra reporting
#1
7
The version of cmd.exe
should actually be pretty irrelevant, unless you try to use features that didn't exist before (in command.com
for example). There is the pseudovariable
cmd的版本。exe实际上应该是非常不相关的,除非您尝试使用以前不存在的特性(例如command.com)。有准变星
%cmdextversion%
which holds the version of the command extensions which has been 2 for ages (at least back to NT 4, iirc).
它保存了命令扩展的版本,这个版本已经有2年的历史了(至少可以追溯到NT 4, iirc)。
But, back to the point: Running ver
and parsing the version string might be your best bet:
但是,回到正题:运行ver并解析版本字符串可能是您最好的选择:
for /f "tokens=2 delims=[]" %%x in ('ver') do set WINVER=%%x
set WINVER=%WINVER:Version =%
#2
15
you can use the "systeminfo" @ cmd.exe
您可以使用“systeminfo”@ cmd.exe
C:\Users\Tagon8>systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name: Microsoft Windows 8 Release Preview
OS Version: 6.2.8400 N/A Build 8400
#3
7
I found a shorter way using ver as well:
我还发现了一种使用ver的更短的方法:
Could be even shorter:
可以更短:
ver | find "5.1" >nul && goto ver_winxp
#4
3
I found a shorter way using ver
as well:
我还发现了一种使用ver的更短的方法:
...
ver | find "5.1"
if %ERRORLEVEL% == 0 goto ver_winxp
...
This will find XP, replace the string with your wanted versions
这将找到XP,用您想要的版本替换字符串
#5
2
Type "ver" at a command prompt.
在命令提示符下输入“ver”。
Next time around, since this isn't really programming related but server or user related, you might try serverfault.com or superuser.com.
下次,由于这与编程无关,而是与服务器或用户有关,您可以尝试serverfault.com或superuser.com。
#6
1
Maybe someone will need the following to determine the SKU (Win7). I'm using some of this script to pick the right OS and XML during sysprep. Hope it helps!
也许有人需要以下内容来确定SKU (Win7)。在sysprep过程中,我使用其中的一些脚本选择正确的OS和XML。希望它可以帮助!
@echo off
set ver=Unknown
systeminfo > C:\sysinfo
findstr /e /c:"Enterprise " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Enterprise
findstr /e /c:"Ultimate " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Ultimate
findstr /e /c:"Professional " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Professional
findstr /e /c:"Home Premium " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Home Premium
findstr /e /c:"Home Basic " C:\sysinfo 1>nul 2>nul
if %errorlevel% equ 0 set ver=Home Basic
del /f /q C:\SP\sysinfo 1>nul 2>nul
Echo Windows 7 %ver%
pause
exit
#7
0
The internal command ver
reports windows version number (which could have been learned by typing help
at the command prompt).
内部命令ver报告windows版本号(可以通过在命令提示符中输入帮助来学习)。
There is a dynamic variable %CMDEXTVERSION%
, but it hasn't progressed in several releases so it's only useful for delineating between Windows NT and Windows 2000 and newer. (Thanks @Joey, here.)
有一个动态变量%CMDEXTVERSION%,但是它在几个版本中都没有进展,所以它只用于描述Windows NT和Windows 2000以及更新版本之间的关系。(谢谢@Joey这里。)
Here's a batch to parse the output of ver for XP and newer, courtesy of Simon Sheppard:
下面是一个解析XP和更新版本的ver输出的批:
@echo off
Setlocal
:: Get windows Version numbers
For /f "tokens=2 delims=[]" %%G in ('ver') Do (set _version=%%G)
For /f "tokens=2,3,4 delims=. " %%G in ('echo %_version%') Do (set _major=%%G& set _minor=%%H& set _build=%%I)
Echo Major version: %_major% Minor Version: %_minor%.%_build%
if "%_major%"=="5" goto sub5
if "%_major%"=="6" goto sub6
Echo unsupported version
goto:eof
:sub5
::Winxp or 2003
if "%_minor%"=="2" goto sub_2003
Echo Windows XP [%PROCESSOR_ARCHITECTURE%]
goto:eof
:sub_2003
Echo Windows 2003 or xp 64 bit [%PROCESSOR_ARCHITECTURE%]
goto:eof
:sub6
if "%_minor%"=="1" goto sub7
Echo Windows Vista or Windows 2008 [%PROCESSOR_ARCHITECTURE%]
goto:eof
:sub7
Echo Windows 7 or Windows 2008 R2 [%PROCESSOR_ARCHITECTURE%]
goto:eof
And here's my own fairly complete, largely academic, kick at the can which returns the parsed version number as environment variables:
这是我自己的一个相当完整的,主要是学术性的,可以返回解析后的版本号作为环境变量:
@echo off
setlocal
:: from http://ss64.org/viewtopic.php?pid=3136#p3136
::==================================
::variables
if %PROCESSOR_ARCHITECTURE%==x86 set pro_arch=32 Bit (x86)
if %PROCESSOR_ARCHITECTURE%==AMD64 set pro_arch=64 Bit (AMD64)
if %PROCESSOR_ARCHITECTURE%==IA64 set pro_arch=Itanium 64 Bit (IA64)
:Main
call :clean
for /f "tokens=2 delims=[]" %%x in ('ver') do set cmdver=%%x
set cmdver=%cmdver:Version =%
call :parse_cmdver
call :ver%cmdver%
call :Report
goto :End
:clean
:: Ensure we don't inherit values from previous runs
set _verCmd=
set _verMajor=
set _verMinor=
set _verBuild=
set _verWin=
goto :eof
:Parse_cmdver
:: Turn "5.1.2306" string into actionable variables
for /f "tokens=1,2,3* delims=." %%g in ("%cmdver%") do (
set major=%%g
set minor=%%h
set build=%%i
)
goto :eof
:Report
echo.
echo. CMD version is %cmdver%
echo. which probably means %longver% %pro_arch%
echo.
goto :eof
:Report2
echo. The numbers are stored in the following variables:
echo.
set _ver
goto :eof
::Table of version numbers built from
:: http://en.wikipedia.org/wiki/Microsoft_Windows#Timeline_of_releases
:ver1.01
set longver=Windows 1.01
set shortver=Win101
goto :eof
:ver2.03
set longver=Windows 2.03
set shortver=Win203
goto :eof
:ver2.10
set longver=Windows 2.10
set shortver=Win21
goto :eof
:ver2.11
set longver=Windows 2.11
set shortver=Win211
goto :eof
:ver3.0
set longver=Windows 3.0
set shortver=Win3
goto :eof
:ver3.1
set longver=Windows 3.1, Windows For Workgroups 3.1, or Windows NT 3.1
set shortver=Win31/WFW31/WinNT31
goto :eof
:ver3.11
set longver=Windows For Workgroups 3.11
set shortver=WFW311
goto :eof
:ver3.2
set longver=Windows 3.2 (released in Simplified Chinese only)
set shortver=Win32ch
goto :eof
:ver3.5
set longver=Windows NT 3.5
set shortver=WinNT35
goto :eof
:ver3.51
set longver=Windows NT 3.51
set shortver=WinNT351
goto :eof
:ver4.0.950
set longver=Windows 95
set shortver=Win95
goto :eof
:ver4.0.1381
set longver=Windows NT 4.0
set shortver=WinNT4
goto :eof
:ver4.90.3000
set longver=Windows Me
set shortver=WinMe
goto :eof
:ver4.10.1998
set longver=Windows 98
set shortver=Win98
goto :eof
:ver4.10.2222
set longver=Windows 98 SE
set shortver=Win98SE
goto :eof
:ver5.0.2195
set longver=Windows 2000
set shortver=Win2K
goto :eof
:ver5.1.2600
set longver=Windows XP or Windows Fundamentals for Legacy PCs
set shortver=WinXP/WinFun
goto :eof
:ver5.2.3790
set longver=Windows XP, Windows XP Pro or Windows Server 2003
set shortver=WinXP/WinXP-Pro/Server2003
goto :eof
:ver5.2.4500
set longver=Windows Home Server
set shortver=WinHomeServer
goto :eof
:ver6.0.6002
set longver=Windows Vista or Windows Server 2008
set shortver=Vista/Server2008
goto :eof
:ver6.1.7600
set longver=Windows 7 or Windows Server 2008 R2
set shortver=Win7/Server2008R2
goto :eof
:End
:: return version to calling shell/script,
:: see http://ss64.com/nt/syntax-functions.html
endlocal & set _verCmd=%cmdver% & set _verMajor=%major% & set _verMinor=%minor% & set _verBuild=%build% & set _verWin=%shortver%
call :Report2 :: comment this line out to suppress extra reporting