Gustav Bertram
Gustav Bertram

Reputation: 14905

How can I find the latest Visual Studio Developer Command Prompt in a batch file?

I need to write a batch file to complete a pre-build step in Visual Studio. As part of it, I need to call the Visual Studio Developer Command Prompt.

I know that for VS2015, the developer command prompt is located in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat.

Unfortunately, our build server has a different version of VS. Is there a simple way to find the latest VSDevCmd.bat for all (or most) versions of Visual Studio in a batch file, so the pre-build step will work on both environments?

Upvotes: 3

Views: 4564

Answers (3)

Micha Wiedenmann
Micha Wiedenmann

Reputation: 20873

You should use vswhere provided by the Microsoft Visual Studio Installer. You copy vswhere.exe to a known location. The wiki has a description on how to start the developer command prompt:

@if not defined _echo echo off
for /f "usebackq delims=" %%i in (`vswhere.exe -prerelease -latest -property installationPath`) do (
  if exist "%%i\Common7\Tools\vsdevcmd.bat" (
    %comspec% /k "%%i\Common7\Tools\vsdevcmd.bat" %*
    exit /b
  )
)

rem Instance or command prompt not found
exit /b 2

Upvotes: 5

dushyantp
dushyantp

Reputation: 4728

The thing that I do for CI-CD machines is use vcvarsall for non 2017 machines and vsdevcmd for 2017 ones. Script snippet below:

rem vsvarsall.bat does not work if there are quoted paths on %PATH%
set path=%path:"=%
rem this will work for non VS 2017 build machines
if exist "c:\progra~2\Micros~1.0\vc\vcvarsall.bat" (
    call c:\progra~2\Micros~1.0\vc\vcvarsall.bat && goto :SetVSEnvFinished
)

echo vcvarsall.bat not found, looking for vsdevcmd.bat
rem Find and run vsdevcmd.bat
set "VS_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2017"

rem The 2017 folder will not be present in Visual Studio 2017 Preview machines (such as 15.8 preview)
if not exist "%VS_PATH%" (
    set "VS_PATH=C:\Program Files (x86)\Microsoft Visual Studio"
)

if not exist "%VS_PATH%" (
    echo "%VS_PATH%" not found. Is Visual Studio installed? && goto :ErrorExit
)

for /f "delims=" %%F in ('dir /b /s "%VS_PATH%\vsdevcmd.bat" 2^>nul') do set VSDEVCMD_PATH=%%F
echo ********Executing %VSDEVCMD_PATH%********
call "%VSDEVCMD_PATH%"
goto :SetVSEnvFinished

:ErrorExit
exit /b 1

:SetVSEnvFinished

So for non 2017 ones, it will execute vcvarsall.bat (which sets up the VS environment). For 2017 versions, it will search for vsdevcmd.bat (in specific folder to reduce search time) and run it. Hope this helps.

Upvotes: 1

Gustav Bertram
Gustav Bertram

Reputation: 14905

I've managed to cobble together a batch file that looks at parameters, then at the filesystem to try to figure out what version of VSDevCmd should be used. $(DevEnvDir) is sometimes *Undefined*, so we need to check for that too.

Tested on VS2015 Professional, VS2017 Professional, and VS2017 Enterprise.

REM Usage in VS build event: call "$(SolutionDir)\find_vsdevcmd.bat" "$(DevEnvDir)"

SET vsversion=

REM Get Visual Studio version, either from command prompt, or newest on filesystem 
if [%1] == [] (
  if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2017 Enterprise"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2017 Professional"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2017 Community"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2015"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2013"
  ) else goto :eof
) else if [%1] == ["*Undefined*"] (
  if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2017 Enterprise"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2017 Professional"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2017 Community"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2015"
  ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" (
    SET vsversion="VS2013"
  ) else goto :eof
) else (
  if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\"] (
    SET vsversion="VS2017 Enterprise"
  ) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\"] (
    SET vsversion="VS2017 Professional"
  ) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\"] (
    SET vsversion="VS2017 Community"
  ) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\" (
    SET vsversion="VS2015"
  ) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\" (
    SET vsversion="VS2013"
  ) else goto :eof
)

if %vsversion% == "VS2017 Enterprise" (
    SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"
    ECHO VS2017 Enterprise
) else if %vsversion% == "VS2017 Professional" (
    SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat"
    ECHO VS2017 Professional
) else if %vsversion% == "VS2017 Community" (
    SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat"
    ECHO VS2017 Community
) else if %vsversion% == "VS2015" (
    SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
    ECHO VS2015
) else if %vsversion% == "VS2013" (
    SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"
    ECHO VS2013
) else goto :eof

call %vsdevcmd%

Upvotes: 0

Related Questions