How to get the Visual Studio installation path in a batch file

9

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.

user565739

Posted 2013-01-21T22:45:16.837

Reputation: 473

Answers

5

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

Adam

Posted 2013-01-21T22:45:16.837

Reputation: 6 454

Pretty sure this will have issues with spaces in install paths. http://stackoverflow.com/questions/22352793/reading-a-registry-value-to-a-batch-variable-handling-spaces-in-value

– Craig Ringer – 2014-03-12T23:16:53.170

3

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\"

user514395

Posted 2013-01-21T22:45:16.837

Reputation:

Is this variable available after the user has installed MSVC at any time? I am trying to figure out a convenient way to look up an MSVC installation to call it's vcvarsall.bat to get further details and paths for the compilers and linker. – Ingwie Phoenix – 2019-05-12T13:53:26.613

1

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

Дмитрий Юдин

Posted 2013-01-21T22:45:16.837

Reputation: 21

Why is the link to the non-English version of the documentation? – Ramhound – 2019-07-24T12:04:03.040

1

It looks like VCInstallDir is an environment variable that is independent upon the version of Visual Studio.

echo %VCInstallDir%

That may be used in a batch file.

Vilhelm H.

Posted 2013-01-21T22:45:16.837

Reputation: 111

0

You can use a method similar to this one to detect the bit level of your particular command shell:

IF "%programfiles%"=="C:\Program Files" (
  ECHO This is a 64-bit cmd.exe shell.
) ELSE (
  ECHO This is a 32-bit cmd.exe shell.
)

Then, you can use that to figure out where Visual Studio is located. It really depends on what you are doing. You could also use a method like this:

SET "VS_LOC=%programfiles%\Visual Studio\bin\program.exe"
IF EXIST "%VS_LOC%" (
  ECHO Found a visual studio installation.
) ELSE (
  ECHO Could not find the visual studio install at %VS_LOC%.
)

I could go on and on about different ways of doing this. It is up to you depending on your situation.

djangofan

Posted 2013-01-21T22:45:16.837

Reputation: 2 459