manipulate the PATH

Started by jkjk12, April 24, 2015, 08:56:36 AM

Previous topic - Next topic

jkjk12

Are there any scripts on this site that would show me how to do the following.     I need to get the PATH statement and then find a string at the beginning.  Then move this string to the end of the PATH.    I need to search for the first backslash I find and grab everything to the Left of it, delete it, and then add it to the end of the PATH.   Any help would be nice.


td

What exactly are you referring to by 'PATH statement'?  Are you referring to the PATH environment variable?  If so then you need to consult the  Environment and EnvironSet function topics in you Consolidated WIL Help file.   The Consolidated WIL Help file is part of your WinBatch installation and can be found on the Start Menu under WinBatch.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

DAG_P6

Unlike many other environment variables, the PATH environment variable is a combination of components that live in two different Registry keys that correspond to the values displayed in the User (upper box) and System (lower box) of the Environment Variables dialog box. It is for this reason that you cannot use SETX.exe. and the like to set the PATH environment variable. To make the change permanent, you must identify which of the two keys contains the substring of interest, then alter one or both keys as needed, bearing in mind that the final PATH string is constructed by appending the value from the user key, HKEY_CURRENT_USER\Environment, to the value read from the system key, HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

For example, on the machine on which I am writing this reply,, the directories listed in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment are as follows.

C:\bin
C:\UTIL
%SystemRoot%
%SystemRoot%\system32
C:\bin\Perl\site\bin
C:\bin\Perl\bin
C:\Program Files (x86)\IDM Computer Solutions\UltraCompare\
C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\
C:\Program Files (x86)\Intel\iCLS Client\
C:\Program Files\Intel\iCLS Client\
%SystemRoot%\System32\Wbem
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Windows Live\Shared
C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86
C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
C:\Program Files\Microsoft SQL Server\120\Tools\Binn\
C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\
C:\Program Files\Common Files\Microsoft Shared\Windows Live
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live

The user portion of the path, stored in HKEY_CURRENT_USER\Environment, contains just one value.

C:\Users\DAVE\Documents

The PATH string displayed in a command window is as follows.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\DAVE>echo PATH = %PATH%
PATH = C:\bin;C:\UTIL;C:\Windows;C:\Windows\system32;C:\bin\Perl\site\bin;C:\bin
\Perl\bin;C:\Program Files (x86)\IDM Computer Solutions\UltraCompare\;C:\Program
Files (x86)\IDM Computer Solutions\UltraEdit\;C:\Program Files (x86)\Intel\iCLS
Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\System32\Wbem;C:\Windows
\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\
Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\Op
enCL SDK\2.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Component
s\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Progra
m Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (
x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft
SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\
;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program
Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Common Files\Micros
oft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Win
dows Live;C:\Users\DAVE\Documents

C:\Users\DAVE>


The net result is that directories input into the SYSTEM path block take precedence over those input into the USER block.

For those who think it is odd that C:\Users\DAVE\Documents is listed in my personal PATH string, it was there because I had to add it to demonstrate how Windows combines the two keys to generate a complete PATH string. In practice, my PATH is fully specified by the SYSTEM key, and my user settings contain no PATH string at all.
David A. Gray
You are more important than any technology.

td

The EnvironSet function's documentation notes the need for registry changes to make permanent changes to the path environment variable, if that is what the OP was inquiring about.   Also, there are examples in the Tech Database that illustrate making permanent changes to the PATH environment variable.   

So as not to appear dismissive, the above dissertation on the PATH environment variable is well stated.  Both the Tech Database and WIL help could be modified to better reflect the information as presented above.  As always thoughtful input is appreciated.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade