12

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:

  • without any duplicate
    (if I add 'xxx' to a PATH like 'aaa;xxx;bbb', I need an updated PATH like 'aaa;bbb;xxx')
  • without any aggregation
    (I can call the script repeatedly without ending up with '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?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Please don't call the Windows command interpreter DOS. It really really isn't the same thing. – Harry Johnston Aug 18 '12 at 22:45
  • @HarryJohnston I agree. The edit made by abatishchev seems to fix that nicely. Thank you abatishchev. – VonC Aug 19 '12 at 07:25
  • @Jay the gool is to only change the environment variable in the CMD local session, *not* the user or system environment variables. So my script is enough in order to allow my users to change *locally* their path (without affecting the same `PATH` set by default by their environment variables) – VonC Aug 20 '12 at 12:05

3 Answers3

7

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 in C:\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 to SET "%~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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The line `set P=%P%;%~2` is critical if your path contains ampersands like in `C:\Documents&Settings`. Better change to `set "P=!P!;%~2"`. The command `SET "%~1=%P%" !` could be simplyfied to `SET "%~1=%P%"` as the trailing exclamation mark has only a (good) effect in delayed expansion mode and if you prepared `%P%` before – jeb Aug 28 '12 at 09:59
  • @jeb good points. I have edited my answer to reflect your comments. – VonC Aug 28 '12 at 10:40
4

The problem isn't as simple as you think. There are a number of issues that can break your code before it ever gets to the end where it needs to return the updated value across the ENDLOCAL barrier.

I already answered this question as an extension to an answer I provided for a similar question. See How to check if directory exists in %PATH%?. In that answer I provide a large list of issues that complicate the problem.
The code at the bottom of the linked answer shows how to reliably add a path if it does not exist in PATH already, and it also demonstrates how to reliably return the value across the ENDLOCAL barrier.

The following edits are from VonC in an attempt to actually put the answer here instead of just a link to the answer. I'll preserve the edit, but I find it difficult to follow without the context of the full linked answer.

[The answer demonstrates how to reliably return the value] using the set "%~1=%var%" ! trick (with the trailing '!')

That thread includes:

That's not clear to me. How can an exclamation mark behind the last quote influence the variable content?

The simple rule for delayed expansion is:
For each character in the line do:

  • If it is a caret (^) the next character has no special meaning, the caret itself is removed
  • If it is an exclamation mark, search for the next exclamation mark (carets are not observed here), then expands to the content of the variable
  • If no exclamation mark is found in this phase, the result is discarded, the result of the phase before is used instead (important for the carets)

So, at this point the difference should be clear, the carets are removed even if the exclamation mark have no other effect in a line.
Example:

@echo off
setlocal EnableDelayedExpansion

echo one caret^^
echo none caret^^  !

set "var1=one caret^"
set "var2=none caret^" !

echo !var1!
echo !var2!
----- OUTPUT ----
one caret^
none caret
one caret^
one caret
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Interesting. +1. I reused the `SET "%~1=%P%" !` part in my answer to actually return properly my updated value outside of the "delayed expansion" function. – VonC Aug 18 '12 at 16:58
2

Yay! Finally got this working with the following test code:

@echo off
Setlocal enabledelayedexpansion

Set p="hello world"

( endlocal & rem return

   Set "a1=%p%"

)

Set a1

This outputs:

a1="hello world"

The reason I used delayed expansion in the test without using any !'s is because it still effects how set works and the batchs I'm testing this for all have delayed expansion.

Thanks for the help guys :o)

PS I tried using the same variable name for both local and external environments but this broke the code. Hence the 2 names used.

Aaron Lowe
  • 33
  • 8
  • 1
    Using `1` (one) as a variable name is a really bad idea, as you can't access it with percent expansion `%1%` will not expand to your variable it will expand the first batch parameter and leave one percent sign in the line – jeb Mar 26 '16 at 18:51
  • Yes I since discovered this also. Have edited the answer. Thanks – Aaron Lowe Apr 01 '16 at 14:53