12

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.

6

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
1
8

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

2
  • Why is the link to the non-English version of the documentation? – Ramhound Jul 24 '19 at 12:04
  • Note: If you're someone who just installed the Visual Studio "build tools" so you can use the command-line compiler (which you still need to find the path to), vswhere is of no particular use, since it will report nothing is installed. – Ron Burk May 18 at 5:41
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\"
1
  • 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 May 12 '19 at 13:53
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.

1
  • This isn't a system wide environment variable, but set by the VS command prompt it seems. In order to access that, you need to find the right command prompt or vcvarsall.bat file, which is basically equivalent to the problem of finding the VS installation path. – CodeManX Aug 14 '20 at 17:27
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.

0

For the small minority just using Microsoft's free command-line compiler (which still is kinda under the umbrella of things called "Visual Studio"), I leave this note. I only installed Visual Studio "Build Tools", and I didn't find a solution here that worked. vswhere.exe didn't believe anything was installed. No particular environment variables were magically set, the tools can be installed in places other than C:, etc. So I was left still wanting a batch file that would automatically locate the compilers/linkers/etc.

What did seem to work was WMI queries, if your batch file is being run by a shell that can do WMI queries, that is. I'm using TCC for my shell (formerly known as "4NT"), and the (built-in) command was as simple as:

wmiquery .  "select InstallLocation from MSFT_VSInstance"

This correctly located the install path even in non-default locations. No idea how far back in legacy-land this might work. PowerShell can certainly do it, but I'm too unfamiliar to quote an equivalent example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.