How to reset environment after starting vcvars32.bat

I am writing a batch file to run some collections using Visual C ++. I would like to β€œundo” the changes to vsvars32.bat / vcvarsXX.bat at the end of the script so that I leave the environment unchanged until the script runs.

Example 1 - using vsvars32.bat

call %VS100COMNTOOLS%vsvars32.bat devenv myProject.sln /Build "Debug|Win32" :: Now undo vsvars32.bat 

Example 2 - using vcvars32.bat and vcvars64.bat

 <path to VC bin>vcvars32.bat :: cmd line build calls for 32 bit application :: Now undo vcvars32.bat <path to VC bin>amd64\vcvars64.bat :: cmd line build calls for 64 bit application :: Now undo vcvars64.bat 

Any suggestions?

+4
source share
2 answers

The solution is simple - SETLOCAL combined with ENDLOCAL. Enter HELP SETLOCAL or HELP ENDLOCAL to learn more about usage.

Example 1:

 setlocal call %VS100COMNTOOLS%vsvars32.bat devenv myProject.sln /Build "Debug|Win32" endlocal 

Example 2:

 setlocal <path to VC bin>vcvars32.bat :: cmd line build calls for 32 bit application endlocal setlocal <path to VC bin>amd64\vcvars64.bat :: cmd line build calls for 64 bit application endlocal 
+10
source

It would be easier if your build.bat gets called:

 cmd.exe /c build.bat 

It will be:

  • Create a child process with a copy of the current environment.
  • Change this environment.
  • Run assembly in a modified child environment.
  • Cancel the child environment when exiting the process.

Therefore, nothing will change in the current environment, and no cleanup is required.

+1
source

All Articles