Delete paths in system environment variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
HugoCar
Posts: 5
Joined: 05 Sep 2020 11:24

Delete paths in system environment variables

#1 Post by HugoCar » 21 Nov 2022 08:03

Hello...

I'm trying to add and delete paths in system environment variables but have had no luck either way
Some commands work to add but not to delete and I would like to do it for both

I need to add 2 routes

MyPath1-> PROGRAM_CONFIG_DIR = %cd% (I have no problem adding or deleting it)
MyPath2-> PATH SYSTEM = %cd%


I searched the web and found this

Code: Select all

@ECHO off
SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath% > system_path_bak.txt
Now I would like to check if MyPath2 is in system_path_bak.txt before adding or deleting it

Create a bat or reg file


Thanks!!

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Delete paths in system environment variables

#2 Post by miskox » 21 Nov 2022 13:16

You could use FIND to search for %cd% in this output file you have:

Code: Select all

@ECHO off
SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO ;%CurrPath%;> system_path_bak.txt
find /i ";%cd%;" system_path_bak.txt>nul||echo CD NOT in CurrPath

REM now we add current dir for testing purposes:
set currpath=%currpath%;%cd%
ECHO ;%CurrPath%;%cd%;> system_path_bak.txt
echo %CD%
find /i ";%cd%;" system_path_bak.txt>nul||echo CD NOT in CurrPath
Note: added semicolons to make sure you don't have partial matches.

Saso

HugoCar
Posts: 5
Joined: 05 Sep 2020 11:24

Re: Delete paths in system environment variables

#3 Post by HugoCar » 23 Nov 2022 00:58

Thanks miskox

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Delete paths in system environment variables

#4 Post by jfl » 04 Dec 2022 08:22

There is a useful script called paths.bat in my System Tools Library that can help you do that:
https://github.com/JFLarvoire/SysToolsL ... sTools.zip

When invoked without any option, the paths.bat script displays the current PATH with 1 pathname per line.
This makes it considerably easier to see what your PATH contains, particularly when it is very long. (Mine has 53 entries!)
You can also filter the output with find or findstr, to look for a needle in the haystack. Ex:

Code: Select all

C:\JFL\Temp>echo %PATH%
C:\Program Files (x86)\VMware\VMware Player\bin\;C:\Program Files\Eclipse Adoptium\jdk-8.0.345.1-hotspot\bin;C:\Program Files\Amazon Corretto\jdk11.0.16_8\bin;C:\ProgramData\chocolatey\bin;C:\JFL\Tools;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Tcl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\JFL\Tools\Win64;C:\JFL\Tools\Win32;C:\JFL\Tools\ezWinPorts\Win64\bin;C:\JFL\Tools\ezWinPorts\Win32\bin;C:\JFL\Tools\UnxUtils\usr\local\wbin;C:\JFL\Tools\GnuWin32\bin;C:\JFL\Tools\SysInternals;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\dotnet\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\jEdit;C:\JFL\SDK\git-subrepo\lib;C:\Program Files\Python39;C:\Program Files\Python39\scripts;C:\Program Files\TortoiseGit\bin;C:\PROGRA~2\GTK\bin;C:\Program Files\Pandoc;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Calibre2\;C:\Program Files\Git\cmd;C:\Program Files\Pandoc\;C:\Program Files\GitHub CLI\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\CMake\bin;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\nodejs\;C:\Program Files (x86)\gsudo\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\PowerShell\7\;C:\Users\Larvoire\scoop\shims;C:\Users\Larvoire\AppData\Local\atom\bin;C:\Users\Larvoire\AppData\Local\Microsoft\WindowsApps;C:\Users\Larvoire\.dotnet\tools;C:\Users\Larvoire\AppData\Local\Markdown Monster;C:\Users\Larvoire\.dotnet\tools;C:\Users\Larvoire\AppData\Local\Yarn\bin;C:\Users\Larvoire\AppData\Roaming\npm

C:\JFL\Temp>paths
C:\Program Files (x86)\VMware\VMware Player\bin\
C:\Program Files\Eclipse Adoptium\jdk-8.0.345.1-hotspot\bin
C:\Program Files\Amazon Corretto\jdk11.0.16_8\bin
[...]
C:\Users\Larvoire\.dotnet\tools
C:\Users\Larvoire\AppData\Local\Yarn\bin
C:\Users\Larvoire\AppData\Roaming\npm

C:\JFL\Temp>paths | findstr /i tools
C:\JFL\Tools
C:\JFL\Tools\Win64
C:\JFL\Tools\Win32
C:\JFL\Tools\ezWinPorts\Win64\bin
C:\JFL\Tools\ezWinPorts\Win32\bin
C:\JFL\Tools\UnxUtils\usr\local\wbin
C:\JFL\Tools\GnuWin32\bin
C:\JFL\Tools\SysInternals
C:\Program Files\Microsoft SQL Server\130\Tools\Binn\
C:\Users\Larvoire\.dotnet\tools
C:\Users\Larvoire\.dotnet\tools

C:\JFL\Temp>
By default, paths.bat manages the PATH variable.
But it can be used with any other variable containing a list of strings separared by semi-colons. For example the PATHEXT, INCLUDE, or LIB variables. Ex:

Code: Select all

C:\JFL\Temp>paths PATHEXT
.COM
.EXE
.BAT
.CMD
.PY
[...]
By default, it manages the local environment variable.
But this can be changed to the system or user variables in the registry, by using the -s and -u options respectively.

And finally, to answer your initial request, paths.bat has options for adding, changing, or removing individual paths in the PATH. (Or in any other similar variable.)
There are also several options for choosing where the new path goes. (Ahead, in the end, or before a given entry.)
Use option -? for displaying a help screen with all options.

For your request, the command to remove the current directory from the system PATH would be something like:

Code: Select all

paths -s -r "%CD%"
For adding the current directory, it'd be:

Code: Select all

paths -s -a "%CD%"
Note that the -a command only adds a path if it's not already there, so you don't have to check that yourself.

Note also that, if you want to know what paths.bat is doing under the hood, you can do a dry-run using the -X (no-execute) option. That will display the command(s) generated that would do the job. Ex:

Code: Select all

C:\JFL\Temp>paths -X -s -r "C:\Program Files (x86)\VMware\VMware Player\bin\"
reg add "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /f /v PATH /d "C:\Program Files\Eclipse Adoptium\jdk-8.0.345.1-hotspot\bin;C:\Program Files\Amazon Corretto\jdk11.0.16_8\bin;C:\ProgramData\chocolatey\bin;C:\JFL\Tools;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Tcl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\JFL\Tools\Win64;C:\JFL\Tools\Win32;C:\JFL\Tools\ezWinPorts\Win64\bin;C:\JFL\Tools\ezWinPorts\Win32\bin;C:\JFL\Tools\UnxUtils\usr\local\wbin;C:\JFL\Tools\GnuWin32\bin;C:\JFL\Tools\SysInternals;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\dotnet\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\jEdit;C:\JFL\SDK\git-subrepo\lib;C:\Program Files\Python39;C:\Program Files\Python39\scripts;C:\Program Files\TortoiseGit\bin;C:\PROGRA~2\GTK\bin;C:\Program Files\Pandoc;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Calibre2\;C:\Program Files\Git\cmd;C:\Program Files\Pandoc\;C:\Program Files\GitHub CLI\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\CMake\bin;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\nodejs\;C:\Program Files (x86)\gsudo\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\PowerShell\7\\"
setx PROCESSOR_ARCHITECTURE -k "HKLM\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE" -m   &:# Broadcast a WM_SETTINGCHANGE message

HugoCar
Posts: 5
Joined: 05 Sep 2020 11:24

Re: Delete paths in system environment variables

#5 Post by HugoCar » 07 Dec 2022 16:18

Hil..!!

There are many interesting things in your zip!!
Thank you jfl

Post Reply