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!