获取VisualStudio命令行脚本路径(VS2019、VS2017、VS2015)

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_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\devenv.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%" "Common7\IDE\devenv.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%!VC\Auxiliary\Build\vcvarsall.bat"
        goto GetVisualStudioEnvScriptEnd
    )

    call :StrFind "%devenv%" "2017" pos
    if not %pos% == -1 (
        set "devenv=!devenv:~0,%dirpos%!VC\Auxiliary\Build\vcvarsall.bat"
        goto GetVisualStudioEnvScriptEnd
    )

    call :StrFind "%devenv%" "Microsoft Visual Studio 14.0" pos
    if not %pos% == -1 (
        set "devenv=!devenv:~0,%dirpos%!VC\vcvarsall.bat""
        goto GetVisualStudioEnvScriptEnd
    )
    set "devenv="
:GetVisualStudioEnvScriptEnd
    ::echo devenv="!devenv!"
    (endlocal & set "%1=%devenv%")
goto :eof

 

输出:

 

posted on 2020-11-17 16:40  dchao  阅读(951)  评论(0编辑  收藏  举报

导航