gpt4 book ai didi

batch-file - 如何在环境变量PATH中搜索和替换字符串?

转载 作者:行者123 更新时间:2023-12-02 17:28:14 27 4
gpt4 key购买 nike

是否有批处理命令来搜索和替换环境变量PATH中的字符串?

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

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


我必须在此变量中搜索 C:\Python24,并且需要将其替换为 C:\Python27

最佳答案

本地PATH替换

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

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


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

Windows在创建新进程(如运行批处理文件的Windows命令处理器进程)时,会创建父进程(Windows资源管理器作为桌面)的环境变量表的副本。对环境变量表所做的每次修改都仅应用于此副本,从而避免对其他已经运行的应用程序造成影响。仅从该过程调用的应用程序会获得修改后的环境变量表(作为新过程启动时作为副本)。



系统路径替换

设置或更改整个系统变量的解决方案是使用命令 setx(扩展设置)。

问题 How to use setx command in a windows batch file关于相同的要求:
如何在整个环境变量PATH系统范围内替换Python版本?

Microsoft在参考页上写了有关命令 setx的信息:


Setx将变量写入注册表中的主环境。使用setx变量设置的变量仅在以后的命令窗口中可用,而在当前命令窗口中不可用。


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

但是,如果仅在系统范围内并且仅在本地计算机上永久且仅在本地计算机上永久更改一次PATH变量,则通过“控制面板-系统”执行此操作通常比使用命令行中的 setx命令更容易。



本地和系统PATH替换示例

这是一个有关如何在本地和系统环境变量PATH中替换Python版本的示例。作为附加功能,如果PATH中缺少Python目录路径,则会添加该路径。

@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%\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


延迟扩展的备用代码:

@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%\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


请注意,PATH不仅是环境变量,而且是内部命令。在命令提示符窗口中键入 help pathpath /?,以获取显示的此小命令的简短帮助。

此处使用命令 pathpath %NEWPATH%更新环境变量PATH的本地实例,并在示例批处理文件的最后一行显示本地环境变量PATH中的目录。

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

但是,命令 path要求带有目录路径的字符串始终不带双引号,即使在字符串中包含空格也仅是因为此命令不希望或不支持除目录路径列表之外的其他选项(以及编写代码的开发人员对于命令 path,如果用户像往常一样将几乎所有其他带双引号的命令包围在字符串中,则命令 %SystemRoot%极有可能不考虑删除字符串开头和结尾的双引号。

上面的批处理文件的缺点是:


本地PATH始终包含已扩展所有环境变量的目录,因此系统PATH不再包含目录列表中的 Python24例如。
在当前命令进程或任何父进程中添加到本地PATH的其他目录也将添加到系统PATH。


有关更新系统PATH的更好解决方案,请参见

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

因此,在本地和系统PATH中用 Python27替换 echo /?的更好的批处理代码是:

@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\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /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%\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


为了了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。


endlocal /?
for /?
goto /?
if /?
path /?
pause /?
rem /?
reg add /?
reg query /?
set /?
setlocal /?
setx /?

关于batch-file - 如何在环境变量PATH中搜索和替换字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com