Reputation: 691
I want to detect windows version from batch and as per result start exe file
Sample code not working
@echo off
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" geq "6.2" goto netfx_4
if "%version%" == "6.1" goto netfx_35
if "%version%" == "6.0" goto netfx_35
:netfx_35
start "C:\Users\Ankur\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\Notepad.lnk"
exit 1
goto :EOF
:netfx_4
start "C:\Users\Ankur\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\Notepad.lnk"
exit 1
goto :EOF
endlocal
for testing on place of exe i start notepad file
but this code is not working.
Upvotes: 2
Views: 2047
Reputation: 43
This code will work from DOS/win3 up to win11 and ready for later versions:
@echo off
rem https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions
REM set errorlevel 1, to check next command
<nul find ""
REM extensions: FOR /f; delayedExpansion: !varNm! value at exec time
setlocal enableextensions enabledelayedexpansion
if NOT ERRORLEVEL 1 goto :WinNew
:WinOld
echo *** OLD OS: Can't enableextensions ***
ver | find " 3." >nul
if ERRORLEVEL 1 echo Win 3.x
ver | find " 4." >nul
if ERRORLEVEL 1 echo Win 9x
rem for pre XP define SystemDrive=C:
if "%SystemDrive%" == "" set SystemDrive=%windir:~0,2%
ver
goto :FIN
:WinNew
rem extract ver content from within []
for /f "tokens=2 delims=][" %%i in ('ver') do set winVersion=%%i
rem extract winVersion and winBuild
for /f "tokens=2-4 delims=. " %%i in ("%winVersion%") do (
set winVersion=%%i%%j& set winBuild=%%k
)
echo *** WinVer:%winVersion%; Build:%winBuild%
set winx=0& set winv=0& set win7=0& set win8=0& set win10=0& set win11=0
set win7up=0& set win8up=0& set win10up=0& set win12up=0
if %winVersion%0 lss 500 goto :WinOld
if %winVersion%0 lss 600 ( set winx=1& goto :okVersion )& rem 5.x: 2000,xp,2003
if %winVersion%0 lss 610 ( set winv=1& goto :okVersion )& rem 6.0: vista
set win7up=1& rem w7 or later
if %winVersion%0 lss 620 ( set win7=1& goto :okVersion )& rem 6.1: w7,2008,2011
set win8up=1& rem w8 or later
if %winVersion%0 lss 630 ( set win8=1& goto :okVersion )& rem 6.2: w8,2012
if %winVersion%0 lss 1000 ( set win8=1& goto :okVersion )& rem 6.3: w81,2012r2
set win10up=1& rem w10 or later
if %winBuild%0 lss 200000 ( set win10=1& goto :okVersion )& rem 10.0 b1xxxx: w10,2016,2019
if %winBuild%0 lss 300000 ( set win11=1& goto :okVersion )& rem 10.0 b2xxxx: w11,2022
set win12up=1& rem future versions here...
:okVersion
echo .
echo . Windows Version: %winVersion%
echo . xp:%winx% vista:%winv% win7:%win7% ^>win7:%win7up% win8:%win8% ^>win8:%win8up% win10:%win10% ^>win10:%win10up% win11:%win11%
:FIN
pause
Upvotes: 0
Reputation: 38604
As the versions you are looking for are OS which use WMIC, I'd go down that route, (especially as the output of ver
is technically the not the OS version).
@ECHO OFF
FOR /F "SKIP=1 TOKENS=1-2 DELIMS=." %%A IN ('WMIC OS GET VERSION'
) DO FOR %%C IN (%%A%%B) DO IF %%C GEQ 62 GOTO :netfx_4
:netfx_35
START "" "%AppData%\Microsoft\Windows\Start Menu\Programs\Accessories\Notepad35.lnk"
GOTO :EOF
:netfx_4
START "" "%AppData%\Microsoft\Windows\Start Menu\Programs\Accessories\Notepad4.lnk"
GOTO :EOF
Upvotes: 1
Reputation: 691
@echo off
setlocal
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" geq "6.2" goto netfx_4
if "%version%" == "6.1" goto netfx_35
if "%version%" == "6.0" goto netfx_35
:netfx_35
start start "" name.exe
exit 1
goto :EOF
:netfx_4
start "" name.exe
exit 1
goto :EOF
endlocal
Note: - EXE should be o the same location of batch file
Upvotes: 2
Reputation: 79993
>=
is an invalid comparison operator. Try geq
(others are equ
neq
lss
leq
gtr
Upvotes: 3