返回列表 发帖

[其他] 批处理根据系统版本、系统类型执行不同操作的几种方法

方法1: net config work
@echo off
REM 关键字           系统类型
REM Windows XP       Windows XP
REM Windows.*2003    Windows 2003
REM Vista            Windows Vista
REM Windows 7        Windows 7
REM Windows.*2008    Windows 2008
net config work > "%temp%\sysinfo_net.txt"
:Win7
findstr /c:"Windows 7" "%temp%\sysinfo_net.txt" > nul
if errorlevel 1 (
    goto :XP
) else (
    echo Windows 7
    REM 把 Win7 系统需要执行的命令放在这里
)
goto :end
:XP
findstr /c:"Windows XP" "%temp%\sysinfo_net.txt" > nul
if errorlevel 1 (
    goto :Other
) else (
    echo Windows XP
    REM 把 XP 系统需要执行的命令放在这里
)
goto :end
:Other
echo 其它系统
REM 把其它系统系统需要执行的命令放在这里
goto :end
:end
pauseCOPY
方法2: ver (某些系统无法准确区分)
@echo off
REM 关键字    系统类型
REM 4         Windows 95
REM 4.10      Windows 98
REM 5.0       Windows 2000
REM 5.1       Windows XP
REM 5.2       Windows Server 2003
REM 5.2       Windows XP x64
REM 6.0       Windows Vista
REM 6.0       Windows Server 2008
REM 6.1       Windows 7
REM 6.1       Windows Server 2008 R2
REM 6.2       Windows 8
REM 6.2       Windows Server 2012
for /f "delims=" %%i in ('ver') do (
    set "version=%%i"
)
set "version=%version:~-9,3%"
if "%version%" equ "6.1" (
    echo Windows 7
    REM 把 Win7 系统需要执行的命令放在这里
) else if "%version%" equ "5.1" (
    echo Windows XP
    REM 把 XP 系统需要执行的命令放在这里
) else (
    echo 其它系统
    REM 把其它系统系统需要执行的命令放在这里
)
pauseCOPY
方法3: systeminfo
@echo off
REM 关键字           系统类型
REM Windows XP       Windows XP
REM Windows.*2003    Windows 2003
REM Vista            Windows Vista
REM Windows 7        Windows 7
REM Windows.*2008    Windows 2008
systeminfo > "%temp%\sysinfo_systeminfo.txt"
:Win7
findstr /c:"Windows 7" "%temp%\sysinfo_systeminfo.txt" > nul
if errorlevel 1 (
    goto :XP
) else (
    echo Windows 7
    REM 把 Win7 系统需要执行的命令放在这里
)
goto :end
:XP
findstr /c:"Windows XP" "%temp%\sysinfo_systeminfo.txt" > nul
if errorlevel 1 (
    goto :Other
) else (
    echo Windows XP
    REM 把 XP 系统需要执行的命令放在这里
)
goto :end
:Other
echo 其它系统
REM 把其它系统系统需要执行的命令放在这里
goto :end
:end
pauseCOPY
方法4: 注册表
@echo off
REM 关键字           系统类型
REM Windows XP       Windows XP
REM Windows.*2003    Windows 2003
REM Vista            Windows Vista
REM Windows 7        Windows 7
REM Windows.*2008    Windows 2008
set "rpath=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
for /f "delims=" %%i in ('reg query "%rpath%" /v ProductName ^| findstr "ProductName"') do (
    set "OS=%%i"
)
:Win7
echo %OS% | findstr /c:"Windows 7" > nul
if errorlevel 1 (
    goto :XP
) else (
    echo Windows 7
    REM 把 Win7 系统需要执行的命令放在这里
)
goto :end
:XP
echo %OS% | findstr /c:"Windows XP" > nul
if errorlevel 1 (
    goto :Other
) else (
    echo Windows XP
    REM 把 XP 系统需要执行的命令放在这里
)
goto :end
:Other
echo 其它系统
REM 把其它系统系统需要执行的命令放在这里
goto :end
:end
pauseCOPY
1

评分人数

我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

不错哦,我一般都是用ver来判断的,systeminfo太慢了……

TOP

返回列表