Windows CMD - Reset path variable from batch file?

I have a batch file that modifies the PATH variable by adding multiple addresses. When the user logs out, PATH is reset to its original value (before the batch file was ever executed). This is normal.

However, if the batch file is run more than once, the same values ​​are re-added, and I get an excessively long redundant PATH variable, which simply increases after each run of the batch.

I would like the reset variable to be what it is when the user logs in before the values ​​are added. I believe the solution is to write the original value in a temporary file and read it back, but is there a better way to do this?

+6
source share
3 answers

Instead of writing the original value to the temp file, you can write it to another environment variable:

 if not defined ORIGINAL-PATH set ORIGINAL-PATH=%PATH% set PATH=c:\extra\stuff;%ORIGINAL-PATH% 

but it would be better to explicitly check if the line you want is already in PATH or not, for example:

 echo %PATH% | findstr /c:"c:\extra\stuff;" > nul || set PATH=c:\extra\stuff;%PATH% 
+6
source

Place @SETLOCAL at the top of your batch file.

Any changes made to the environment will be restored when you exit the batch file.

Run setlocal /? for more details.

+6
source

I have been looking for a solution for a long time for a similar problem. Finally, I ended up using pathmgr.cmd, which I downloaded from:

http://gallery.technet.microsoft.com/Batch-Script-To-Manage-7d0ef21e

To use it to clear a custom PATH, the following parameters can be used from the command line:

pathmgr.cmd / clean / user / p / y

Other useful options are also available.

+1
source

Source: https://habr.com/ru/post/895213/


All Articles