Programmatically finding the VS2017 installation directory - visual-studio

With previous versions of VS you could query the registry to determine the installation directory for VS:
HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0
However, this doesn't seem to work with the VS2017 RC. We have scripts that detect the latest installed VS and then do "the right thing", and so far I'm having issues plugging VS2017 into those systems.
Does anyone know how to programmatically determine the installation location for VS2017?

You can use vswhere tool to get VS2017 location.
Example:
#echo off
rem VS2017U2 contains vswhere.exe
if "%VSWHERE%"=="" set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
set InstallDir=%%i
)
if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
"%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)
You can read more about it here: https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/

Visual Studio 2017 supports no-registry, side-by-side installations of all SKUs (Enterprise, Professional and Community).
MSI installlers can query via APIs described here:
https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/
Examples are here:
https://code.msdn.microsoft.com/Visual-Studio-Setup-0cedd331
https://github.com/microsoft/vs-setup-samples

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!

Well, vswhere.exe doesn't really supply more than the Visual Studio edition installation path. Here's my .profile file Interix snippet from 2008 doing the same with a minor update (shell script):
if [[ -n $PROCESSOR_ARCHITEW6432 || $PROCESSOR_ARCHITECTURE != "x86" ]]; then
hkeybase='HKLM\SOFTWARE\Wow6432Node\Microsoft\'
else
hkeybase='HKLM\SOFTWARE\Microsoft\'
fi
for vsver in "15.0" "14.0" "12.0" "11.0" "10.0" "9.0" "8.0"; do
_vsinstalldir=$(reg.exe query ${hkeybase}'VisualStudio\SxS\VS7' -v $vsver 2>/dev/null \
| sed -n 's|.*REG_SZ *\([ [:print:]]*\).*|\1|p' | sed 's|\\|/|g')
if [[ -n $_vsinstalldir ]]; then break; fi
done; unset vsver
That's enumerating Visual Studio installations favouring the latest in registry key
HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7
Still working for Visual Studio 2017. Would be easy to translate to cmd syntax. To query the registry is simpler and doesn't require vswhere.exe in your path, thus favourable IMO.
Now finding the current Visual C++ instance and the SDKs is another task entirely. :D
Common output in case you wonder:
C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/

I had a devil of time trying to modify Srekel's answer to search for only VS2017. Note: If you put the "for" statement below inside an "if" block it will wreck the escape characters and won't work.
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)"
)
set vswherestr=^"!ProgramFiles(x86)!\Microsoft Visual Studio\Installer\vswhere.exe^" -version [15.0,16.0^^) -products * -requires Microsoft.Component.MSBuild -property installationPath
for /f "usebackq tokens=*" %%i in (`!vswherestr!`) do (
set BUILDVCTOOLS=%%i\Common7\Tools
echo BUILDVCTOOLS: !BUILDVCTOOLS!
if not exist !BUILDVCTOOLS!\VsDevCmd.bat (
echo Error: Cannot find VS2017 Build Tools
goto :buildfailed
)
call "!BUILDVCTOOLS!\VsDevCmd.bat"
)

May I recommend my package get-vs2017-path it uses only built-in Windows tools (and although it's built as an NPM package, it has no dependencies, and the tools folder works standalone)

See following answer in here:
https://stackoverflow.com/a/55754831/2338477
You can use either command line tool for querying Visual studio location, but I also provide programmatic way to query Visual Studio location. Code is based upon vswhere source code, but simplified.

We have only 3 Visual Studio editions.
\Community
\Professional
\Enterprise
As such we can simplify everything a bit.
Here some tested CMD script.
#SET env_all_vs2017_root=%ProgramFiles(x86)%\Microsoft Visual Studio\2017
#SET env_vs2017_path="%env_all_vs2017_root%\Professional"
#IF NOT EXIST %env_vs2017_path% SET env_vs2017_path="%env_all_vs2017_root%\Community"
#IF NOT EXIST %env_vs2017_path% SET env_vs2017_path="%env_all_vs2017_root%\Enterprise"
#REM Let's fail laudly
#IF NOT EXIST %env_vs2017_path% SET "env_vs2017_path=Visual Studio 2017 install path was not found by %~nx0"
#REM You may want to remove quotes
#SET unquoted=%env_vs2017_path:"=%
#REM And now let's see the result and PAUSE
#ECHO VS 2017 install path is
#ECHO %unquoted%
#PAUSE

You can use this PowerShell snippet for finding the VS2017 installation directory:
$vssetup_path = "$([Environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\Modules\VSSetup"
if (-not (Test-Path $vssetup_path -pathType container))
{
iwr https://github.com/Microsoft/vssetup.powershell/releases/download/1.0.36-rc/VSSetup.zip -OutFile "$env:TEMP\VSSetup.zip"
Expand-Archive "$env:TEMP\VSSetup.zip" $vssetup_path
}
$vs2017 = Get-VSSetupInstance -All | Select-VSSetupInstance -Require 'Microsoft.VisualStudio.Workload.NativeDesktop' -Version '[15.0,16.0)' -Latest
"Installation Path: " + $vs2017.InstallationPath
#`vsdevcmd.bat -arch=x86` or `vsdevcmd.bat -arch=amd64` can be used to setup path's to VC++ compiler
"VsDevCmd.bat Path: " + $vs2017.InstallationPath + "\Common7\Tools\VsDevCmd.bat"

I use powershell like KindDragon suggested
$Is64bitOs = $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64';
if ($Is64bitOs){
$registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0";
}
else {
$registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0";
}
$VSInstallDir = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0 -Name ShellFolder).ShellFolder;

Related

Anaconda Installation: ERROR: The system was unable to find the specified registry key or value

I installed anaconda and setup a new environment from a yml file.
That is fine.
Then i tried to open the prompt window of my new environment and i get the following output every single time.
C:\WINDOWS\system32>SET DISTUTILS_USE_SDK=1
C:\WINDOWS\system32>SET MSSdk=1
C:\WINDOWS\system32>SET platform=
C:\WINDOWS\system32>IF /I [AMD64] == [amd64] set "platform=true"
C:\WINDOWS\system32>IF /I [] == [amd64] set "platform=true"
C:\WINDOWS\system32>if defined platform (set
"VSREGKEY=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0"
) ELSE (set
"VSREGKEY=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0" )
C:\WINDOWS\system32>for /F "skip=2 tokens=2,*" %A in ('reg query
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0"
/v InstallDir') do SET "VSINSTALLDIR=%B" ERROR: The system was unable
to find the specified registry key or value.
C:\WINDOWS\system32>if "" == "" (set "VSINSTALLDIR=" )
C:\WINDOWS\system32>if "" == "" ( ECHO "WARNING: Did not find VS in
registry or in VS140COMNTOOLS env var - your compiler may not work"
GOTO End ) "WARNING: Did not find VS in registry or in VS140COMNTOOLS
env var - your compiler may not work" The system cannot find the batch
label specified - End
I have no clue how to fix this. Any help ?
My answer is about showing the steps of the proposed solution of #Arpan and that is deleting the batch file named "vs2015_compiler_vars".
How I managed to solve this in Windows
1) Set option to view hidden files
2) Go to this location
C:\ProgramData\Anaconda3\envs\tensorflow_env\etc\conda\activate.d
3) Delete batch file named "vs2015_compiler_vars"
======================================================
How I think to solve this in Linux
Find the file named "vs2015_compiler_vars" and delete it.
As sugested by #Arpan, the file may be found at
$CONDAHOME/etc/conda/activate.d
As suggested by #Arpan removing vs2015_compiler_vars from $CONDAHOME/etc/conda/activate.d helped.
launch Anaconda navigator-->under environments tab --> select uninstalled from the dropdown--> then search for the installed Visual studio version VS and enable it.. apply and exit
then try reinstall teraflow and keras from command..
should work

Batch file- Find Microsoft Visual C++ 2008 Redistributable Setup in a Folder using Batch Script

How do I check in my current folder that:
Microsoft Visual C++ 2008 Redistributable setup exists using a batch script ,
Since it is an .exe file I guess I need to do something so that I could read the file description.
I can parse through the list of available files those exists in folder as:
for /r %%i in (*) do echo %%i
the name of the file is
vcredist_x86_2008
Can some one do this for
Microsoft_VC90_CRT_x86
which is a .msm file
Thanks.
How about:
dir *Redistributable*.exe
if ERRORLEVEL 1 echo Error
Basically, you issue dir command and then check for status ( i.e. whether dir found something or not ) using ERRORLEVEL. If dir was successful then ERRORLEVEL will have 0 value.

Check Visual Studio Shell Installation from Batch

How can we check if and which version of Visual Studio Shell is installed, from a batch script?
I understand we can check the existence of file/folder say under
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE
But I am looking for a more elegant and generic solution.
Any help?
Update to accepted answer:
Your answer is elegant and does the task. Since I was specifically checking for certain versions, I am using (after checking the link you provided):
#echo off
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.10.0" >> nul 2>&1
if %ERRORLEVEL% NEQ 0 ( echo VS 2010 not installed ) else ( echo VS 2010 installed. )
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.11.0" >> nul 2>&1
if %ERRORLEVEL% NEQ 0 ( echo VS 2012 not installed ) else ( echo VS 2012 installed. )
#echo off
for /d %%a in ("%programfiles%\Microsoft Visual Studio*") do (
for /f "tokens=3 delims=\" %%x in ("%%a") do echo %%x
)
pause >nul
If you need more details there are plenty of reg keys you can query to get more info but that would be much harder to extract the data you wanted from the keys and values.
Note: If you are running on x64 then you may need to add a check for %systemdrive%\Program Files (x86) depending on where VS is installed.

How to find Windows SDK's SetEnv.cmd / SetEnv.cmd Does not work correctly

We have a Team City Build Server running and want to compile a Visual C++ project. So far this would be easy, since I've setup our Windows Build Agent with the Windows SDK, but we don't have a solution / project file.
The project files are instead created with CMake. CMake seems to be a little bit dumb (can't generate Solution when Visual Studio is not installed), but with some tricks, I could get it to do it. The solution can then be built with MSBuild.
And here comes the problem. For this to work automatically, I need to call the Windows SDK's SetEnv.cmd. And I can't seem to find it automatically. It's in the bin sub directory of the Windows SDK, but neither bin nor the root are in the path, and the %mssdk% environment variable is set by the SetEnv.cmd and is not available beforehand!
Adding the Windows SDK\bin dir to the PATH leads to SetEnv.cmd no longer working (exits with a message like The x86 compilers are not currently installed and Jump target Set_x86 not found.
The start menu link is calling the SetEnv.cmd with the Windows SDK dir as working directory instead. But if I add the root directory to the PATH, Bin\SetEnv.cmd is not available.
How can I find SetEnv.cmd automatically? Even setting an environment variable to the full path of the setenv.cmd doesn't work, and when I define %mssdk% as the sdk dir, then call %mssdk%\bin\SetEnv doesn't work as well. I also tried to define %mssdk%, then cd %mssdk%, then calling bin\SetEnv. Also compilers not found in all these cases. It also doesn't work if I manually cd to the root or bin dir on a command line and then call SetEnv.cmd...
The start menu link works fine though.
For the record, my solution for now, as strange as this is, is the following:
I created a MSBuild file that creates the solution file with CMake on the command line, then invokes the created solution with a MSBuild task. The MSBuild file can be easily built from TeamCity, though I needed some additional tricks to satisfy CMake's stupid looking for the compiler, though I won't invoke it thing. Not really satisfying, but it works.
My solution (sets %WindowsSdkPath%, so that SetEnv.cmd could be found under %WindowsSdkPath%Bin\):
#ECHO OFF
IF "%WindowsSdkVersion%"=="" (
CALL :SetWindowsSdkVersionHelper HKCU > nul 2>&1
IF ERRORLEVEL 1 CALL :SetWindowsSdkVersionHelper HKLM > nul 2>&1
IF ERRORLEVEL 1 GOTO ERROR_NOWSDK
)
CALL :SetWindowsSdkPathHelper > nul 2>&1
IF ERRORLEVEL 1 GOTO ERROR_NOWSDK
GOTO END
:SetWindowsSdkPathHelper
SET WindowsSdkPath=
FOR /F "tokens=1,2*" %%i in ('REG QUERY "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\%WindowsSdkVersion%" /V InstallationFolder') DO (
IF "%%i"=="InstallationFolder" (
SET "WindowsSdkPath=%%k"
)
)
IF "%WindowsSdkPath%"=="" EXIT /B 1
EXIT /B 0
:SetWindowsSdkVersion
CALL :GetWindowsSdkVersionHelper HKCU > nul 2>&1
IF ERRORLEVEL 1 CALL :GetWindowsSdkVersionHelper HKLM > nul 2>&1
IF ERRORLEVEL 1 EXIT /B 1
EXIT /B 0
:SetWindowsSdkVersionHelper
SET WindowsSdkVersion=
FOR /F "tokens=1,2*" %%i in ('REG QUERY "%1\SOFTWARE\Microsoft\Microsoft SDKs\Windows" /V "CurrentVersion"') DO (
IF "%%i"=="CurrentVersion" (
SET "WindowsSdkVersion=%%k"
)
)
IF "%WindowsSdkVersion%"=="" EXIT /B 1
EXIT /B 0
:ERROR_NOWSDK
ECHO The Windows SDK %WindowsSdkVersion% could not be found.
EXIT /B 1
:END
I was inspired for this by the SetEnv.cmd itself...
Mac, nice answer!
Now I would like to run msbuild with my project file. But before I should run SetEnv.Cmd - right?
So, here we go:
run_Macs_code.bat REM see above
call "%WindowsSdkPath%\bin\Setenv.cmd" /Release /x86 /xp
cd E:\client
msbuild client.proj
Now it's working :)

Command line tool to delete folder with a specified name recursively in Windows?

I want to delete every "_svn" in every folder and subfolder...
For example
c:\
proyect1
_svn
images
_svn
banner
_svn
buttons
_svn
Then I run something like
rm-recurse c:\proyect1 _svn
And I should get:
c:\
proyect1
images
banner
buttons
The ideal thing would be a tiny stand-alone EXE or something like that.
--
Thanks Grant, as soon as I posted the question I saw SVN documentation about the SVN export command, but I also want to delete the _vti_* folders stuff Visual Studio creates, so I'll also explore the for solution.
Similar to BlackTigerX's "for", I was going to suggest
for /d /r . %d in (_svn) do #if exist "%d" rd /s/q "%d"
Time to learn some PowerShell ;o)
Get-ChildItem -path c:\projet -Include '_svn' -Recurse -force | Remove-Item -force -Recurse
The first part finds each _svn folder recursively. Force is used to find hidden folders.
Second part is used to delete these folders and their contents.
Remove commandlet comes with a handy "whatif" parameter which allows to preview what will be done.
PowerShell is available for Windows XP and Windows Vista. It is present on Windows 7 and on Windows Server 2008 R2 by default.
It's a MS product, it's free, and it rocks!
For inclusion/invocation from within a BATCH file use (say for removing Debug and Release folder):
for /d /r . %%d in (Debug Release) do #if exist "%%d" echo "%%d" && rd /s/q "%%d"
double % are required within a batch file to work as escape chars. Else it reports error of syntax.
Thanks.
for /f "usebackq" %d in (`"dir _svn /ad/b/s"`) do rd /s/q "%d"
http://ebersys.blogspot.com/2008/07/recursively-delete-svn-folders-easy-way.html
In Windows? If you are using tortoiseSVN you can use the export command to export a copy of the project without the .svn/_svn folders.
import os
import shutil
curdir = os.path.abspath(os.path.dirname(__file__))
def removedir(dirname, name = ".svn"):
if os.path.isdir(dirname):
for file in os.listdir(dirname):
if os.path.isdir(os.path.join(dirname, file)) and file == name:
thedir = os.path.join(dirname, name)
shutil.rmtree(thedir)
print ".",
else:
removedir(os.path.join(dirname, file))
I think you can try this Python script, which will work under any OS if you've got Python installed.
Here... with FreeCommander or TotalCommander
http://www.broobles.com/blog/posts/36
socendani
Another option from SVN Forum: use XCopy with a file that contains the list of files/directories to be excluded (.svn or _svn in this case)
XCopy C:\VersionedFolder C:\UnVersionedFolder /EXCLUDE:C:\No.SVN.txt /E /C /I /F /R /Y

Resources