GetVisualStudioEnvScript.bat
@echo off setlocal enabledelayedexpansion call :GetVisualStudioEnvScript devenv echo devenv="!devenv!" if "%1" == "" ( pause ) exit /b 0 ::获取字符串长度 ::%1 string ::%2 len :StrLen setlocal enabledelayedexpansion set len=0 set str=%~1 :StrLenLoop if not "!str:~%len%!"=="" ( set /A len+=1 goto :StrLenLoop ) (endlocal & set %2=%len%) goto :eof ::查找字符串 ::%1 string ::%2 token ::%3 pos :StrFind setlocal enabledelayedexpansion set "pos=0" call :Strlen %2 len set "str=%~1" set "token=%~2" ::echo str=%str% token=%token% :StrFindLoop if "!str:~%pos%,%len%!"=="" ( (endlocal & set %3=-1) goto :eof ) else if not "!str:~%pos%,%len%!"=="!token!" ( set /A pos+=1 goto :StrFindLoop ) if "!str:~%pos%,%len%!"=="!token!" ( (endlocal & set %3=%pos%) ) else ( (endlocal & set %3=-1) ) goto :eof ::获取VisualStudio脚本 ::%1 devenv :GetVisualStudioEnvScript setlocal enabledelayedexpansion for /f "Delims=" %%i in ('REG QUERY "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Pathsdevenv.exe"') do ( set "value=%%i" set "value=!value:*reg_sz=!" for /f "tokens=*" %%b in ("!value!") do set devenv=%%~fb ) ::echo devenv="%devenv%" call :StrFind "%devenv%" "Common7IDEdevenv.exe" dirpos if %dirpos% == -1 ( echo Visual Studio Version Unknown : "%devenv%" set "devenv=" goto GetVisualStudioEnvScriptEnd ) call :StrFind "%devenv%" "2019" pos if not %pos% == -1 ( set "devenv=!devenv:~0,%dirpos%!VCAuxiliaryBuildvcvarsall.bat" goto GetVisualStudioEnvScriptEnd ) call :StrFind "%devenv%" "2017" pos if not %pos% == -1 ( set "devenv=!devenv:~0,%dirpos%!VCAuxiliaryBuildvcvarsall.bat" goto GetVisualStudioEnvScriptEnd ) call :StrFind "%devenv%" "Microsoft Visual Studio 14.0" pos if not %pos% == -1 ( set "devenv=!devenv:~0,%dirpos%!VCvcvarsall.bat"" goto GetVisualStudioEnvScriptEnd ) set "devenv=" :GetVisualStudioEnvScriptEnd ::echo devenv="!devenv!" (endlocal & set "%1=%devenv%") goto :eof
输出: