Windows batch os version check with if support

旧巷老猫 提交于 2019-12-24 18:50:16

问题


I'm making "BatchGameHub" for windows 10, and right now I'm making installer in .batch, but I don't know how to check windows version (10, 8, 7) and with support with if command. Example what I mean: (Command to check version)

if %winversion%== 10 goto start
goto win_not_compatible

:win_not_compatible
echo.
echo Your windows version cannot run gamehub!
echo [ Press any key to cancel installer... ]
echo.
pause>null
exit

回答1:


Check the windows build numbers - https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx

And try this:

@echo off

for /f "tokens=4,5 delims=,. "  %%a in ('ver') do set "build_n=%%a.%%b"

set "Windows10=10.0"
set "Windows8.1=6.3"
set "Windows8=6.2"
set "Windows7=6.1"
set "WindowsVista=6.0"

if exist "%windir%\sysWOW64\" (
 set "bitness=64bit"
) else (
 set "bitness=32bit"
)

if not "%Windows10%"=="%build_n%" (
  goto :not_supported
) else (
  goto :start
)

:not_supported

echo not supported
exit /b 1

:start
pause



回答2:


To do what your question asks using WMIC this should work:

@Echo Off
Set "OV="
For /F "Skip=1 Tokens=*" %%A In (
    'WMIC OS Where "Version<'4'" Get Version 2^>Nul') Do For %%B In (%%A
) Do Set "OV=%%A"
If Defined OV GoTo Start
Echo=
Echo Your windows version cannot run gamehub!
Echo [ Press any key to cancel installer... ]
Echo=
Pause>Nul
Exit /B

:Start

I used < with 4 to GoTo Start only on Windows 10, because a string comparison will find that the first character, 1 is less than 4 etc. (Windows 10 or newer.)




回答3:


A batch file like this could be used:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Get version of Windows enclosed in square brackets from command VER.
for /F "tokens=2 delims=[]" %%I in ('ver') do set "VersionWindows=%%I"

rem Get major and minor version and build number from version information.
for /F "tokens=2-4 delims=. " %%A in ("%VersionWindows%") do (
    set "VersionMajor=%%A"
    set "VersionMinor=%%B"
    set "VersionBuild=%%C"
)

if %VersionMajor% LSS 10 (
    echo/
    echo Your windows version cannot run gamehub!
    echo/
    echo [Press any key to cancel installer... ]
    echo/
    endlocal
    pause >nul
    exit
)

rem Other commands for installation like:

set "ProcessArchitecture=%PROCESSOR_ARCHITECTURE:~-2%"
if %ProcessArchitecture% == 86 (
    set "ProcessArchitecture=32"
) else (
    if exist %SystemRoot%\Sysnative\cmd.exe set "ProcessArchitecture=32"
)

echo Your are using Windows version %VersionMajor%.%VersionMinor%.%VersionBuild% %PROCESSOR_ARCHITECTURE%
echo The installation process is running in %ProcessArchitecture%-bit environment.

endlocal

Please take into account the Microsoft Developer Network (MSDN) articles:

  • WOW64 Implementation Details
  • File System Redirector
  • Registry Keys Affected by WOW64

It depends on the process starting the batch file if the batch file is executed by 32-bit cmd.exe in %SystemRoot%\SysWOW64 or by 64-bit cmd.exe in %SystemRoot%\System32 on Windows x64 (AMD64).

The following Wikipedia articles could be also helpful for this coding task:

  • List of Microsoft Windows versions
  • Windows Environment Variables

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • ver /?

And the name of the device NUL is nul with just one L. A line like pause>null results in redirecting the message into file with name null instead of device NUL. See also the MSDN articles Using Command Redirection Operators and Naming Files, Paths, and Namespaces containing among lots of other useful information also a list of device names.

And last but not least read also DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ and avoid in future echo. in batch files and read about debugging a batch file.



来源:https://stackoverflow.com/questions/49513153/windows-batch-os-version-check-with-if-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!