如何在环境变量 PATH 中搜索和替换字符串? [英] How to search and replace a string in environment variable PATH?

查看:34
本文介绍了如何在环境变量 PATH 中搜索和替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何命令可以批量搜索和替换环境变量PATH中的字符串?

Is there any command in batch to search and replace a string in environment variable PATH?

例如环境变量PATH的内容为:

For example the contents of environment variable PATH is:

C:windowssystem32;C:windows;C:windowsSystem32Wbem;C:windowsSystem32WindowsPowerShellv1.0;C:Program FilesAccuRevin;C:Python24

我必须在这个变量中搜索 C:Python24 并且需要用 C:Python27 替换它.

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

推荐答案

本地路径替换

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

Local PATH replacement

The solution provided by wmz is perfect:

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

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

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?

微软在参考页面上写了关于命令 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 脚本,则还必须使用命令 set 修改本地 PATH.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 变量的更改应该在系统范围内完成,并且只在本地计算机上永久完成一次,通常通过 控制面板 - 系统 更容易做到这一点strong> 比使用命令行中的命令 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 %NEWPATH% 更新环境变量 PATH 的本地实例并在本地显示目录环境变量 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. 本地 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 中将 Python24 替换为 Python27 的更好的批处理代码是:

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/?
  • 转到/?
  • 如果/?
  • 路径/?
  • 暂停/?
  • rem/?
  • reg 添加/?
  • reg 查询/?
  • 设置/?
  • setlocal/?
  • setx/?

这篇关于如何在环境变量 PATH 中搜索和替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆