Board logo

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

作者: Batcher    时间: 2015-3-2 16:03     标题: 批处理根据系统版本、系统类型执行不同操作的几种方法

方法1: net config work
  1. @echo off
  2. REM 关键字           系统类型
  3. REM Windows XP       Windows XP
  4. REM Windows.*2003    Windows 2003
  5. REM Vista            Windows Vista
  6. REM Windows 7        Windows 7
  7. REM Windows.*2008    Windows 2008
  8. net config work > "%temp%\sysinfo_net.txt"
  9. :Win7
  10. findstr /c:"Windows 7" "%temp%\sysinfo_net.txt" > nul
  11. if errorlevel 1 (
  12.     goto :XP
  13. ) else (
  14.     echo Windows 7
  15.     REM 把 Win7 系统需要执行的命令放在这里
  16. )
  17. goto :end
  18. :XP
  19. findstr /c:"Windows XP" "%temp%\sysinfo_net.txt" > nul
  20. if errorlevel 1 (
  21.     goto :Other
  22. ) else (
  23.     echo Windows XP
  24.     REM 把 XP 系统需要执行的命令放在这里
  25. )
  26. goto :end
  27. :Other
  28. echo 其它系统
  29. REM 把其它系统系统需要执行的命令放在这里
  30. goto :end
  31. :end
  32. pause
复制代码
方法2: ver (某些系统无法准确区分)
  1. @echo off
  2. REM 关键字    系统类型
  3. REM 4         Windows 95
  4. REM 4.10      Windows 98
  5. REM 5.0       Windows 2000
  6. REM 5.1       Windows XP
  7. REM 5.2       Windows Server 2003
  8. REM 5.2       Windows XP x64
  9. REM 6.0       Windows Vista
  10. REM 6.0       Windows Server 2008
  11. REM 6.1       Windows 7
  12. REM 6.1       Windows Server 2008 R2
  13. REM 6.2       Windows 8
  14. REM 6.2       Windows Server 2012
  15. for /f "delims=" %%i in ('ver') do (
  16.     set "version=%%i"
  17. )
  18. set "version=%version:~-9,3%"
  19. if "%version%" equ "6.1" (
  20.     echo Windows 7
  21.     REM 把 Win7 系统需要执行的命令放在这里
  22. ) else if "%version%" equ "5.1" (
  23.     echo Windows XP
  24.     REM 把 XP 系统需要执行的命令放在这里
  25. ) else (
  26.     echo 其它系统
  27.     REM 把其它系统系统需要执行的命令放在这里
  28. )
  29. pause
复制代码
方法3: systeminfo
  1. @echo off
  2. REM 关键字           系统类型
  3. REM Windows XP       Windows XP
  4. REM Windows.*2003    Windows 2003
  5. REM Vista            Windows Vista
  6. REM Windows 7        Windows 7
  7. REM Windows.*2008    Windows 2008
  8. systeminfo > "%temp%\sysinfo_systeminfo.txt"
  9. :Win7
  10. findstr /c:"Windows 7" "%temp%\sysinfo_systeminfo.txt" > nul
  11. if errorlevel 1 (
  12.     goto :XP
  13. ) else (
  14.     echo Windows 7
  15.     REM 把 Win7 系统需要执行的命令放在这里
  16. )
  17. goto :end
  18. :XP
  19. findstr /c:"Windows XP" "%temp%\sysinfo_systeminfo.txt" > nul
  20. if errorlevel 1 (
  21.     goto :Other
  22. ) else (
  23.     echo Windows XP
  24.     REM 把 XP 系统需要执行的命令放在这里
  25. )
  26. goto :end
  27. :Other
  28. echo 其它系统
  29. REM 把其它系统系统需要执行的命令放在这里
  30. goto :end
  31. :end
  32. pause
复制代码
方法4: 注册表
  1. @echo off
  2. REM 关键字           系统类型
  3. REM Windows XP       Windows XP
  4. REM Windows.*2003    Windows 2003
  5. REM Vista            Windows Vista
  6. REM Windows 7        Windows 7
  7. REM Windows.*2008    Windows 2008
  8. set "rpath=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
  9. for /f "delims=" %%i in ('reg query "%rpath%" /v ProductName ^| findstr "ProductName"') do (
  10.     set "OS=%%i"
  11. )
  12. :Win7
  13. echo %OS% | findstr /c:"Windows 7" > nul
  14. if errorlevel 1 (
  15.     goto :XP
  16. ) else (
  17.     echo Windows 7
  18.     REM 把 Win7 系统需要执行的命令放在这里
  19. )
  20. goto :end
  21. :XP
  22. echo %OS% | findstr /c:"Windows XP" > nul
  23. if errorlevel 1 (
  24.     goto :Other
  25. ) else (
  26.     echo Windows XP
  27.     REM 把 XP 系统需要执行的命令放在这里
  28. )
  29. goto :end
  30. :Other
  31. echo 其它系统
  32. REM 把其它系统系统需要执行的命令放在这里
  33. goto :end
  34. :end
  35. pause
复制代码

作者: 522235677    时间: 2015-3-3 08:32

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




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2