gpt4 book ai didi

windows - 用 bat 更改 PATH 变量?

转载 作者:可可西里 更新时间:2023-11-01 10:36:14 27 4
gpt4 key购买 nike

我正在尝试编写 2 个 BATCH 工具来改变我的系统变量路径。第一个应该在 Path 前面添加一个目的地,但之前将旧路径保存在 txt 文件中,第二个 BATCH 文件应该从 txt 文件加载路径并将路径替换为保存的路径以再次恢复旧路径.

@echo. 2>%date:~0%_PATH.txt
@echo %PATH% >> %date:~0%_PATH.txt
@echo "This tool will change the system variable PATH"
@echo "if you want to continue, press any key."
@echo "To cancel press CTRL+C"
@pause

@setx /m PATH "C:\MinGW\bin;%PATH%"
@echo.
@echo.
@echo "The System variables has been set"
@echo "Your original PATH variable was saved to: %date:~0%_PATH.txt"

@pause

这是第一个批处理文件,应该在 PATH 前面添加 C:\MinGW\bin。 (这不是重点,在我运行批处理文件后,不仅仅是我的目标。

@ECHO OFF
ECHO "You are about to restore your system variable PATH to a previous state!"
ECHO "If you want to preform this step, press any key."
ECHO "To cancel the process, press CTRL+C";
ECHO.
SET index=1

SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%f IN (*.*) DO (
SET file!index!=%%f
ECHO !index! - %%f
SET /A index=!index!+1
)

SETLOCAL DISABLEDELAYEDEXPANSION

SET /p selection="Please select by typing the number which file you want to recover your PATH:"

SET file%selection% > nul 2>&1

IF ERRORLEVEL 1 (
ECHO Invalid number selected!!
EXIT /B 1
)

CALL :RESOLVE %%file%selection%%%
ECHO Selected file name: %file_name%

GOTO :EOF

:RESOLVE
SET file_name=%1
GOTO :EOF

@setx /m PATH %file_name%

这是恢复批处理文件,不会选择我选择的文件。

我希望有人能帮助我。谢谢

最佳答案

SETX 只改变注册表中的值,不改变当前shell环境中的变量。正如文档在您运行 SETX/? 时所说:

NOTE: 1) SETX writes variables to the master environment in the registry.
2) On a local system, variables created or modified by this tool
will be available in future command windows but not in the
current CMD.exe command window.

如果您只想在当前环境中修改 PATH 的值,只需使用常规的 SET 命令即可。如果您想同时执行这两项操作,则需要同时使用这两个命令。

关于windows - 用 bat 更改 PATH 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182793/

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