The Computer Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2

--

Chapters
00:00 How To Get The Visual Studio Installation Path In A Batch File
00:41 Answer 1 Score 13
01:01 Accepted Answer Score 6
01:27 Answer 3 Score 2
02:13 Answer 4 Score 0
02:30 Answer 5 Score 0
03:35 Thank you

--

Full question
https://superuser.com/questions/539666/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#batch #path #batchfile #visualstudio

#avk47



ANSWER 1

Score 13


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




ACCEPTED ANSWER

Score 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



ANSWER 3

Score 2


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



ANSWER 4

Score 0


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.




ANSWER 5

Score 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.