Skip to content

Instantly share code, notes, and snippets.

@n00bmind
Created November 17, 2022 19:55
Show Gist options
  • Save n00bmind/fb014cbfccb7c11fbddc0b9c2ce9ba6c to your computer and use it in GitHub Desktop.
Save n00bmind/fb014cbfccb7c11fbddc0b9c2ce9ba6c to your computer and use it in GitHub Desktop.
Find vcvars64.bat script and execute it, then cache the results in an .env file that can be immediately imported next time
@echo off
set CACHED_ENV=W:\env\cached_vcvars.env
set PRE_ENV="%TEMP%\pre.env"
set POST_ENV="%TEMP%\post.env"
:: Look for a cached env
if exist %CACHED_ENV% (
echo Applying previously cached env in '%CACHED_ENV%'..
echo (just delete that file if you want to recreate it, ***for example if the system's %%PATH%% must be updated***^)
FOR /F "tokens=*" %%i in (%CACHED_ENV%) do set %%i
goto :END
)
echo Couldn't find cached env. Trying to locate vcvars batch file..
echo.
if not defined VCDIR (
set VCDIR=""
)
if not exist %VCDIR% (
set VCDIR="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
)
if not exist %VCDIR% (
set VCDIR="C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools"
)
if not exist %VCDIR% (
set VCDIR="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community"
)
if not exist %VCDIR% (
set VCDIR="C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools"
)
if not exist %VCDIR% (
if exist "%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
for /F "tokens=* USEBACKQ" %%F in (`"%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath`) do set VCDIR="%%F"
)
)
if not exist %VCDIR% (
echo Couldn't find Visual Studio folder!
goto :END
)
:: Store env before running vars command
set | sort /rec 65535 > %PRE_ENV%
REM sort %PRE_ENV%
call %VCDIR%\VC\Auxiliary\Build\vcvars64.bat
:: Store new env and compute diff using 'comm' (included in git-for-windows)
set | sort /rec 65535 > %POST_ENV%
REM sort %POST_ENV%
comm -13 %PRE_ENV% %POST_ENV% > %CACHED_ENV%
goto :END
:END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment