Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 461 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to search and replace a string in environment variable PATH?

#1
Is there any command in batch to search and replace a string in environment variable __PATH__?

For example the contents of environment variable __PATH__ is:

C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\AccuRev\bin;C:\Python24

I have to search for `C:\Python24` in this variable and need to replace it by `C:\Python27`.
Reply

#2
Local PATH replacement
----------------------

The solution provided by [wmz](

[To see links please register here]

) is perfect:

set "PATH=%PATH:Python24=Python27%"

For details about replacements in values of environment variables run in a command prompt window `set /?` or look on the Microsoft documentation page for Windows command [set](

[To see links please register here]

).

Windows creates a copy of the environment variables table of the parent process (*Windows Explorer* as desktop) on creation of a new process like the *Windows Command Processor* process running a batch file. Every modification made on the environment variables table are applied only on this copy which avoids impacts on other already running applications. Just applications called from this process get the modified environment variables table (as copy when started as new process).

---

System PATH replacement
-----------------------

The solution to set or change a variable system wide is the usage of command [setx](

[To see links please register here]

) (set extended).

The question [How to use setx command in a windows batch file](

[To see links please register here]

) is about same requirement:
How to replace the version of Python in environment variable **PATH** system wide?

Microsoft wrote on referenced page about command `setx`:

> **Setx** writes variables to the master environment in the registry. Variables set with setx variables are available **in future command windows only**, not in the current command window.

This means if Python scripts are executed from same console window after using `setx`, the local **PATH** must be also modified using command `set`. Other instances of environment variable **PATH** of other console or GUI processes already running on using `setx` to change the list of directories are not modified by `setx`. Those processes must be terminated and restarted to take note of changes made on master environment table.

But if the change of the **PATH** variable should be done system wide and permanently on local computer only and only once, it is often easier to do this via **Control Panel - System** than doing it with command `setx` from command line.

---

Local and system PATH replacement example
-----------------------------------------

Here is an example on how to replace version of Python in local and system environment variable **PATH**. As an extra feature the path of Python directory is appended if missing in **PATH**.

@echo off

rem Replace Python24 by Python27 in local environment variable PATH.
set "NEWPATH=%PATH:Python24=Python27%"

rem Append C:\Python27 to PATH if not containing C:\Python27 at all.
if "%NEWPATH:Python27=%" == "%PATH%" if "%NEWPATH:~-1%" == ";" (set "NEWPATH=%PATH%C:\Python27") else set "NEWPATH=%PATH%;C:\Python27"

rem Update PATH in system environment table.
%SystemRoot%\System32\setx.exe PATH "%NEWPATH%" /M

rem Update local PATH in this console window. (Important: No double quotes!)
path %NEWPATH%

rem Remove local environment variable NEWPATH as not needed anymore.
set NEWPATH=

rem Output value of local environment variable PATH.
path

Alternate code with delayed variable expansion being more fail-safe:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Replace Python24 by Python27 in local environment variable PATH.
set "NEWPATH=!PATH:Python24=Python27!"

rem Append C:\Python27 to PATH if not containing C:\Python27 at all.
if "!NEWPATH:Python27=!" == "!PATH!" if "!NEWPATH:~-1!" == ";" (set "NEWPATH=!PATH!C:\Python27") else set "NEWPATH=!PATH!;C:\Python27"

rem Update PATH in system environment table.
%SystemRoot%\System32\setx.exe PATH "!NEWPATH!" /M

rem Update local PATH in this console window. (Important: No double quotes!)
endlocal & path %NEWPATH%

rem Output value of local environment variable PATH.
path

Please note that **PATH** is not only an environment variable, but also an internal command. Type in a command prompt window either `help path` or `path /?` to get short help for this little command displayed.

The command `path` is used here to update local instance of environment variable **PATH** with `path %NEWPATH%` and to display the directories in local environment variable **PATH** with last line of the example batch file.

On line with command `setx` the double quotes around `%NEWPATH%` are very important as **PATH** most likely contains also one or more directory paths with a space inside. Therefore the entire string which should be assigned to system environment variable **PATH** must be in double quotes.

But the command `path` requires the string with the directory paths always without surrounding double quotes even with space characters inside the string simply because this command does not expect or support other options than the list of directory paths (and the developer who wrote the code for command `path` did most likely not think about removing the double quotes at beginning and end of the string if a user surrounded the string as usual for nearly all other commands with double quotes).

The disadvantages of the batch files above are:

1. Local __PATH__ contains the directories always with all environment variables already expanded and therefore system __PATH__ does not have anymore for example `%SystemRoot%` in list of directories.

2. Additional directories added to local __PATH__ in current command process or any parent process would be also added to system __PATH__.

A better solution to update system __PATH__ is explained in answer on

[Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](

[To see links please register here]

)

So a better batch code to replace `Python24` by `Python27` in local and system __PATH__ is:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Define a variable Separator with a semicolon as value if
rem the local PATH string does not end already with a semicolon.
if "!PATH:~-1!" == ";" (set "Separator=") else set "Separator=;"

rem Replace Python24 by Python27 in local environment variable PATH.
set "LocalPath=!PATH:Python24=Python27!"

rem Append C:\Python27 to local PATH if not containing C:\Python27 at all.
if "!LocalPath:Python27=!" == "!PATH!" set "LocalPath=!PATH!%Separator%C:\Python27"

rem Update local PATH for this command process in this console window.
endlocal & set "PATH=%LocalPath%"

rem Output value of local environment variable PATH.
path

rem Get system PATH directly from Windows registry to get
rem the directory list with not expanded environment variables.
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^>nul') do (
if /I "%%G" == "Path" (
set "SystemPath=%%I"
if defined SystemPath goto CheckPath
)
)
echo Error: System environment variable PATH not found with a non-empty value.
echo(
pause
exit /B

:CheckPath
setlocal EnableExtensions EnableDelayedExpansion
rem Define a variable Separator with a semicolon as value if
rem the system PATH string does not end already with a semicolon.
if "!SystemPath:~-1!" == ";" (set "Separator=") else set "Separator=;"

rem Replace Python24 by Python27 in system environment variable PATH.
set "NewPath=!SystemPath:Python24=Python27!"

rem Append C:\Python27 to system PATH if not containing C:\Python27 at all.
if "!NewPath:Python27=!" == "!SystemPath!" set "NewPath=!SystemPath!%Separator%C:\Python27"

rem Update system PATH if any change is necessary at all. Command SETX
rem is by default not installed on Windows XP and truncates string values
rem longer than 1024 characters to 1024 characters. So command REG is used
rem to update system PATH in Windows registry if SETX is not available or
rem new system PATH is too long. Administrator privileges are requirend in
rem any case for updating persistent stored system PATH in Windows registry.
if not "!SystemPath!" == "!NewPath!" (
set "UseSetx=1"
if not "!NewPath:~1024,1!" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe Path "!NewPath!" /M >nul
) else (
set "ValueType=REG_EXPAND_SZ"
if "!NewPath:%%=!" == "!NewPath!" set "ValueType=REG_SZ"
%SystemRoot%\System32\reg.exe ADD "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /f /v Path /t !ValueType! /d "!NewPath!" >nul
)
)

rem Output value of system environment variable PATH.
echo PATH=!NewPath!

endlocal

To understand the commands used and how they work, open a [command prompt](

[To see links please register here]

) window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

+ `echo /?`
+ `endlocal /?`
+ `exit /?`
+ `for /?`
+ `goto /?`
+ `if /?`
+ `path /?`
+ `pause /?`
+ `rem /?`
+ `reg add /?`
+ `reg query /?`
+ `set /?`
+ `setlocal /?`
+ `setx /?`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through