My Technical Notes

Monday, 1 May 2017

BAT File to load Program after setting up Environment Variables

You can use a BAT file to set environment variables before invoking a program. A shortcut can then be made to this BAT file.

One BAT file has the logic which sets the environment variables and invokes a program path passed in as an argument (`SetEnv_Invoke.bat`):


@echo off

REM set all the user environment variables
SET USERPROFILE=C:\TEMP\Users\Tahir
SET APPDATA=C:\TEMP\Users\tahir\AppData\Roaming
SET LOCALAPPDATA=C:\TEMP\Users\tahir\AppData\Local
SET TEMP=C:\TEMP\Users\tahir\AppData\Local\Temp
SET TMP=C:\TEMP\Users\tahir\AppData\Local\Temp

REM invoke the program
start "" %1
exit

For each application we want to set up, we create another BAT file which invokes `SetEnv_Invoke.bat` passing in the path of the program to be executed. For instance we could create a BAT file to load up Chrome (`InvokeChrome.bat`):


CALL "SetEnv_Invoke.bat" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

Note that both `SetEnv_Invoke.bat` and `InvokeChrome.bat` have to be in the same folder for this to work.

No comments: