Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the value of a variable outside a Windows batch script which uses "delayed expansion local" mode?

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?

like image 305
VonC Avatar asked Aug 18 '12 16:08

VonC


People also ask

How to clear variables after each batch script run?

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

How do I use local variables in a batch script?

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.

What is the use of delayed expansion in C++?

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.

When to use environment variables in a batch file?

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.


Video Answer


1 Answers

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.

like image 89
VonC Avatar answered Sep 16 '22 19:09

VonC