How to get the Visual Studio installation path in a batch file
Normally, the path is something like C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\
.
From Visual Studio, one can use $(VCInstallDir)$
to get this path.
Q: But in a batch file, how to get this path?
I know one can use environment variable %VS100COMNTOOLS%
in a batch file
to get a similar path which is C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\
.
The solution should not dependent on VS's version.
You could also use the registry to find the path to the Visual Studio install directory. You would have to add extra logic to handle different versions of VS that might be installed e.g 10.0 or 11.0.
@ECHO OFF
setlocal ENABLEEXTENSIONS
; 32-bit system:
set KEY_NAME="SOFTWARE\Microsoft\VisualStudio\9.0\Setup\VS"
; 64-bit system:
; set KEY_NAME="SOFTWARE\WOW6432Node\Microsoft\VisualStudio\9.0\Setup\VS"
set VALUE_NAME=ProductDir
FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueValue=%%C
)
if defined ValueName (
@echo Registry Value = %ValueValue%
) else (
@echo %KEY_NAME%\%VALUE_NAME% not found.
)
pause
Use vswhere utility which is a part of VS bundle (officially), but can also be used apart of MSVC.
> vswhere.exe -latest -property installationPath
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
Here you may find more details
Way late to this question, but i've found a simpler way to get the MSVC directory. The trick is to use %VS100COMNTOOLS%
variable (or the version of your visual studio, here 100
is 10.0
), which is guaranteed to exist even without calling the ..\VC\vcvarsall.bat
batch file.
%VCInstallDir%
variable falls to this as it's empty until vcvarsall.bat
is called, but we can't call the file if we don't know the full path.
The %VS100COMNTOOLS%
on the other hand exists and returns something like:
c:\Program Files\Microsoft Visual Visual Studio 10.0\Common7\Tools
Then, a simply cut-off of the last characters seems good:
echo "%VS100COMNTOOLS:~0,-14%VC\"