在bat脚本中区分操作系统版本

来源:互联网 发布:淘宝评论打不开 编辑:程序博客网 时间:2024/05/01 09:40
@echo offset OsVersion=0set OsProcessor=0echo 操作系统版本:ver|findstr /r /i " [版本 5.1.*]" > NUL && goto WindowsXPver|findstr /r /i " [版本 6.1.*]" > NUL && goto Windows7goto UnknownVersion:WindowsXPset OsVersion="WindowsXP"goto GetProcessor:Windows7set OsVersion="Windows7"goto GetProcessor:UnknownVersionset OsVersion="UnknownVersion"goto GetProcessor:GetProcessorif /i "%processor_architecture%" equ "x86" (set OsProcessor="X86") else (if /i "%processor_architecture%" equ "amd64" (set OsProcessor="X64") else (set OsProcessor="UnknownProcessor"))echo %OsVersion% %OsProcessor%pause


使用ver|findstr /r /i " [版本 5.1.*]" > NUL后环境变量errorlevel中保存的退出码是cmd.exe的,不是findstr.exe

使用ver|findstr /r /i " [版本 5.1.*]" > NUL && goto WindowsXP时用的是findstr.exe的退出码

0 0