且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何搜索和替换环境变量 PATH 中的字符串?

更新时间:2022-05-02 09:20:02

本地 PATH 替换

wmz 提供的解决方案是完美的:

Local PATH replacement

The solution provided by wmz is perfect:

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

有关在命令提示符窗口中运行的环境变量值替换的详细信息 set/? 或查看 Windows 命令的 Microsoft 文档页面 设置.

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.

Windows 在创建新进程(如运行批处理文件的 Windows 命令处理器进程)时创建父进程(Windows 资源管理器作为桌面)的环境变量表的副本.对环境变量表所做的每项修改仅应用于此副本,以避免影响其他已运行的应用程序.只是从这个进程调用的应用程序获得修改后的环境变量表(作为新进程启动时的副本).

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).

在系统范围内设置或更改变量的解决方案是使用命令 setx(设置扩展).

The solution to set or change a variable system wide is the usage of command setx (set extended).

如何在 windows 批处理文件中使用 setx 命令 的问题大致相同:
如何在系统范围内的环境变量 PATH 中替换 Python 的版本?

The question How to use setx command in a windows batch file is about same requirement:
How to replace the version of Python in environment variable PATH system wide?

Microsoft 在有关命令 setx 的参考页面上写道:

Microsoft wrote on referenced page about command setx:

Setx 将变量写入注册表中的主环境.使用 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.

这意味着如果在使用 setx 后从同一个控制台窗口执行 Python 脚本,本地 PATH 也必须使用命令 set 修改.setx 不会修改已经使用 setx 更改目录列表的其他控制台或 GUI 进程的环境变量 PATH 的其他实例.必须终止并重新启动这些进程以记录对主环境表所做的更改.

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.

但是,如果 PATH 变量的更改应该在系统范围内且仅在本地计算机上永久完成并且仅一次,则通过 控制面板 - 系统 来执行此操作通常会更容易强> 比从命令行使用命令 setx 来做.

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.

这里是一个关于如何在本地和系统环境变量PATH中替换Python版本的示例.如果 PATH 中缺少 Python 目录的路径,则会附加一个额外的功能.

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%" set "NEWPATH=%PATH%;C:Python27"

rem Update PATH in system environment table.
%SystemRoot%System32setx.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

延迟扩展的替代代码:

@echo off
setlocal 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!" set "NEWPATH=!PATH!;C:Python27"

rem Update PATH in system environment table.
%SystemRoot%System32setx.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

请注意,PATH 不仅是一个环境变量,还是一个内部命令.在命令提示符窗口中键入 help pathpath/? 以获取有关显示的这个小命令的简短帮助.

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.

命令 path 用于更新环境变量 PATH 的本地实例为 path %NEWPATH% 并在本地显示目录环境变量 PATH 和示例批处理文件的最后一行.

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.

在命令 setx 中,%NEWPATH% 周围的双引号非常重要,因为 PATH 很可能还包含一个或多个目录路径里面有空间.因此,应分配给系统环境变量 PATH 的整个字符串必须用双引号括起来.

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.

但是命令 path 需要带有目录路径的字符串,即使字符串中有空格字符,也始终没有双引号,因为该命令不期望或不支持目录路径列表以外的其他选项(以及为命令 path 编写代码的开发人员很可能不会考虑删除字符串开头和结尾的双引号,如果用户像往常一样将字符串括在几乎所有其他带有双引号的命令中引号).

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 包含的目录始终包含所有已展开的环境变量,因此系统 PATH 不再具有例如 %SystemRoot%代码>在目录列表中.

  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.

在当前命令进程或任何父进程中添加到本地PATH的其他目录也将添加到系统PATH.

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

更新系统 PATH 的更好解决方案在

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

为什么其他文件夹路径也用SetX添加到系统PATH中,而不仅仅是指定的文件夹路径?

所以在本地和系统PATH中用Python27替换Python24的更好的批处理代码是:

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.
set "Separator="
if not "!PATH:~-1!" == ";" 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*" %%N in ('%SystemRoot%System32
eg.exe query "HKLMSystemCurrentControlSetControlSession ManagerEnvironment" /v "Path" 2^>nul') do (
    if /I "%%N" == "Path" (
        set "SystemPath=%%P"
        if defined SystemPath goto CheckPath
    )
)
echo Error: System environment variable PATH not found with a non-empty value.
echo/
pause
goto :EOF

: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.
set "Separator="
if not "!SystemPath:~-1!" == ";" 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%System32setx.exe set "UseSetx="
    if defined UseSetx (
        %SystemRoot%System32setx.exe Path "!NewPath!" /M >nul
    ) else (
        set "ValueType=REG_EXPAND_SZ"
        if "!NewPath:%%=!" == "!NewPath!" set "ValueType=REG_SZ"
        %SystemRoot%System32
eg.exe ADD "HKLMSystemCurrentControlSetControlSession ManagerEnvironment" /f /v Path /t !ValueType! /d "!NewPath!" >nul
    )
)

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

endlocal

要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo/?
  • endlocal/?
  • for/?
  • 转到/?
  • if/?
  • 路径/?
  • 暂停/?
  • rem/?
  • reg add/?
  • reg 查询/?
  • set/?
  • setlocal/?
  • setx/?