gpt4 book ai didi

batch-file - 将当前目录永久添加到 Windows 路径

转载 作者:行者123 更新时间:2023-12-04 03:03:14 25 4
gpt4 key购买 nike

我正在尝试将当前目录(从命令行)永久添加到 Windows 路径,但在实现此操作时遇到严重问题。

我最初的尝试是:

set PATH=%PATH%;%cd%

但是,这仅在当前 session 中有效;一旦我关闭命令行窗口,PATH 环境变量就会保留其以前的值。

接下来,我尝试了:

setx PATH=%PATH%;%cd%

根据我在此处找到的一些答案,这可能适用于 Windows 7 和 8,但在 Windows 10 中,setx 命令具有三种工作方式:

Syntax 1:
SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]

Syntax 2:
SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]

Syntax 3:
SETX [/S system [/U [domain\]user [/P [password]]]]
/F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]

长话短说,我无法让它工作:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).

如何以最简单的方式完成我的目标?

如果每个 Windows 版本都有不同的语法,那么我也很乐意获得此信息。

最佳答案

通过简单地使用覆盖或附加存储在注册表中的PATH的文件夹路径来从批处理文件中修改系统用户 PATH是不好的本地 PATH,如 Why are other folder paths also added to system PATH with SetX and not only the specified folder path? 的答案中详细描述

此任务的一种解决方案是将当前目录路径添加到用户 PATH 是使用以下代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe query "HKEY_CURRENT_USER\Environment" /v "Path" 2^>nul') do (
if /I "%%G" == "Path" (
set "UserPath=%%I"
if defined UserPath goto CheckPath
)
)

set "UseSetx=1"
if not "%CD:~1024,1%" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe Path "%CD%" >nul
) else (
%SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t REG_SZ /d "%CD%" >nul
)

endlocal
exit /B

:CheckPath
setlocal EnableDelayedExpansion
if "!UserPath:~-1!" == ";" (set "Separator=") else "Separator=;"
set "PathCheck=!UserPath!%Separator%"
if not "!PathCheck:%CD%;=!" == "!PathCheck!" goto EndBatch
if not "!PathCheck:%CD%\;=!" == "!PathCheck!" goto EndBatch
set "PathToSet=!UserPath!%Separator%%CD%"
set "UseSetx=1"
if not "!PathToSet:~1024,1!" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe Path "!PathToSet!" >nul
) else (
set "ValueType=REG_EXPAND_SZ"
if "!PathToSet:%%=!" == "!PathToSet!" set "ValueType=REG_SZ"
%SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t !ValueType! /d "!PathToSet!" >nul
)
:EndBatch
endlocal
endlocal

此解决方案的缺点是用户 PATH 最终成为 C:\Temp;C:\Temp\Other Folder;C:\Temp\One More Folder (当当前目录是第一个 C:\Temp 时,在下次运行批处理文件时 C:\Temp\Other FolderC:\Temp\One More Folder 在第三次执行批处理文件时。

避免这种情况的解决方案是在下一个批处理文件 MyAppPath 中定义一个特定于应用程序的环境变量,该变量在执行批处理文件时始终会被覆盖。如果 user PATH 中尚不存在对环境变量 MyAppPath 的引用,则仅向 user PATH 添加对环境变量 PATH 的引用。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "UseSetx=1"
if not "%CD:~1024,1%" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe MyAppPath "%CD%" >nul
) else (
%SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v MyAppPath /t REG_SZ /d "%CD%" >nul
)

set "UserPath="
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe query "HKEY_CURRENT_USER\Environment" /v "Path" 2^>nul') do (
if /I "%%G" == "Path" (
set "UserPath=%%I"
if defined UserPath goto CheckPath
)
)

if exist %SystemRoot%\System32\setx.exe (
%SystemRoot%\System32\setx.exe Path "%%MyAppPath%%" >nul
) else (
%SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t REG_EXPAND_SZ /d "%%MyAppPath%%" >nul
)
endlocal
exit /B

:CheckPath
setlocal EnableDelayedExpansion
if "!UserPath:~-1!" == ";" (set "Separator=") else set "Separator=;"
if "!UserPath:%%MyAppPath%%=!" == "!UserPath!" (
set "PathToSet=!UserPath!%Separator%%%MyAppPath%%"
set "UseSetx=1"
if not "!PathToSet:~1024,1!" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe Path "!PathToSet!" >nul
) else (
%SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t REG_EXPAND_SZ /d "!PathToSet!" >nul
)
)
endlocal
endlocal

在这种情况下,存储在注册表中的用户 %MyAppPath% 始终仅包含 MyAppPath,并且注册表值的类型为 REG_EXPAND_SZ。环境变量 MyAppPath 的值也存储在注册表中,但类型为 REG_SZPATH 的值在每次执行批处理文件时更新为当前目录路径。因此,每次从不同文件夹执行批处理文件时,注册表中的用户 PATH 不会变得越来越长。

一般来说,如果应用程序或应用程序套件的应用程序文件夹或其子文件夹之一在执行应用程序或套件中的任何应用程序时必须位于本地 App Paths 中才能正常工作,则该应用程序或应用程序套件的编码不好全部。应用程序或应用程序套件还可以将其安装路径存储在注册表中的其他位置,例如 %APPDATA%PATH 子文件夹中的文件(用户帐户相关的标准应用程序数据路径),下次运行时可以从中读取它。仅当此应用程序很可能主要由用户在命令提示符窗口中执行时,安装程​​序包才应修改用户系统 echo /?

要了解所使用的命令及其工作原理,请打开 command prompt 窗口,在其中执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。

  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • reg /?
  • reg add /?
  • reg query /?
  • set /?
  • setlocal /?
  • setx /?
  • >nul

还应阅读以下内容:

关于batch-file - 将当前目录永久添加到 Windows 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47070156/

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