Context: I need to call a Windows batch script which would update my PATH
by adding another path 'xxx
' at the end of it, but:
xxx
' to a PATH like 'aaa;xxx;bbb
', I need an updated PATH
like 'aaa;bbb;xxx
')aaa;bbb;xxx;xxx;xxx;...
')What I have tried:
The following function takes care of any duplicate and does the job
:cleanAddPath -- remove %~1 from PATH, add it at the end of PATH
SETLOCAL ENABLEDELAYEDEXPANSION
set PATH=!PATH:%~2=!
set PATH=!PATH:;;=;!
set PATH=%PATH%;%~2
set P=!P:;;=;!
echo %PATH%
echo -------------
ENDLOCAL
exit /b
But, it needs delayed expansion local mode, which means: at the end of the script (or here, at the end of the function cleanAddPath
), whatever has been set for %PATH%
is thrown away.
I could ask the users (for which I write the script) to launch their cmd
with a cmd /V:ON
option (activating the delayed expansion, otherwise off by default), but that is not practical.
How can I modify the PATH
variable the way I described above, and still have it updated in my current DOS session after calling said script?
How to clear variables after each batch script run? 1 Windows batch: data from file disappears after delayed expansion 1 Windows batch - Delayed expansion removes exclamation mark 1 average number of file lines from CMD 0 Use Double Delayed Expansion to SET a variable in a batch file
Batch Script - Local Variables in Functions. Local variables in functions can be used to avoid name conflicts and keep variable changes local to the function. The SETLOCAL command is first used to ensure the command processor takes a backup of all environment variables. The variables can be restored by calling ENDLOCAL command.
As pointed in the answer the main usage of the delayed expansion is the setting and accessing variables in brackets context. Though it can be useful in another situations too.
Working with Environment Variables. If you have variables that would be used across batch files, then it is always preferable to use environment variables. Once the environment variable is defined, it can be accessed via the % sign.
The page "DOS - Function Collection" gives great example on how a function can return a value in DOS, even when using delayed expansion mode:
The following function will update any variable you want with an addition PATH
:
:cleanAddPath -- remove %~2 from %~1, add it at the end of %~1
SETLOCAL ENABLEDELAYEDEXPANSION
set P=!%~1!
set P=!P:%~2=!
set P=!P:;;=;!
set P=!P!;%~2
set P=!P:;;=;!
(ENDLOCAL & REM.-- RETURN VALUES
SET "%~1=%P%"
)
exit /b
Note the concatenation of paths using. As jeb comments:
The line
set P=%P%;%~2
is critical if your path contains ampersands like inC:\Documents&Settings
.
Better change to set "P=!P!;%~2
".
The SET "%~1=%P%"
is the part which allows to memorize (in the variable represented by %~1
) the value you have set using delayed expansion features.
I initially used SET "%~1=%P%" !
, but jeb comments:
The command
SET "%~1=%P%" !
could be simplified toSET "%~1=%P%"
as the trailing exclamation mark has only a (good) effect in delayed expansion mode and if you prepared%P%
before.
To update your PATH
variable, you would call your function with:
call :cleanAddPath PATH "C:\my\path\to\add"
And it will persists after leaving that script, for your current DOS session.
dbenham's answer points to a more robust answer (upvoted), but in my case this script is enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With