With previous versions of VS you could query the registry to determine the installation directory for VS:
HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Visu
KindDragon's solution didn't quite work for me due to batch's "delayed expansion" "feature". (WAT)
Here is my code, compatible with VS 2017 15.2 (for the vswhere.exe installation)
SETLOCAL EnableDelayedExpansion
if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)
for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
set InstallDir=%%i
)
if exist "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat" (
call "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
) else (
echo "Could not find !InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
)
Especially note usage of SETLOCAL EnableDelayedExpansion and !InstallDir!
We have only 3 Visual Studio editions.
As such we can simplify everything a bit.
Here some tested CMD script.
@SET env_all_vs2017_root=%ProgramFiles(x86)%\Microsoft Visual Studio\2017
@SET env_vs2017_path="%env_all_vs2017_root%\Professional"
@IF NOT EXIST %env_vs2017_path% SET env_vs2017_path="%env_all_vs2017_root%\Community"
@IF NOT EXIST %env_vs2017_path% SET env_vs2017_path="%env_all_vs2017_root%\Enterprise"
@REM Let's fail laudly
@IF NOT EXIST %env_vs2017_path% SET "env_vs2017_path=Visual Studio 2017 install path was not found by %~nx0"
@REM You may want to remove quotes
@SET unquoted=%env_vs2017_path:"=%
@REM And now let's see the result and PAUSE
@ECHO VS 2017 install path is
@ECHO %unquoted%
@PAUSE
Visual Studio 2017 supports no-registry, side-by-side installations of all SKUs (Enterprise, Professional and Community).
MSI installlers can query via APIs described here: https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/
Examples are here:
May I recommend my package get-vs2017-path it uses only built-in Windows tools (and although it's built as an NPM
package, it has no dependencies, and the tools folder works standalone)
See following answer in here:
https://stackoverflow.com/a/55754831/2338477
You can use either command line tool for querying Visual studio location, but I also provide programmatic way to query Visual Studio location. Code is based upon vswhere
source code, but simplified.
You can use vswhere tool to get VS2017 location.
Example:
@echo off
rem VS2017U2 contains vswhere.exe
if "%VSWHERE%"=="" set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
set InstallDir=%%i
)
if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
"%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)
You can read more about it here: https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/