gpt4 book ai didi

windows - 使用批处理脚本在当前 cmd 中设置环境

转载 作者:可可西里 更新时间:2023-11-01 10:44:48 26 4
gpt4 key购买 nike

我在发布这个问题之前确实搜索过答案,但我找不到任何东西。

问题是,我有一个 CMD shell,可以从中启动不同的应用程序。我想要的是执行一个可以修改当前cmd环境的bat文件。

在我们当前的设置中,我们正在使用 call 来启动批处理文件,但该环境不会在 callers cmd 环境中更新。是否有在当前 cmd shell 中运行 Batch 的命令?

最佳答案

“批处理文件具有简单设置和 setx。当从命令提示符执行批处理文件时,它会打开一个新实例并运行 bat。因此不会在主 cmd 上设置 env 更改。”

setsetx在运行批处理文件的命令提示符下都不生效。

  • set 仅适用于当前命令提示符(这是属于批处理文件的命令提示符不是运行批处理文件)。当批处理文件终止时,这些更改将丢失。

  • setx 只影响新的命令提示符(而不是那些已经打开的命令提示符,比如用于运行批处理文件的命令提示符。您只会看到这些如果您启动一个全新的命令提示符,则会发生变化。

您需要做的是拥有一个单个批处理文件,该文件使用set 并且启动您的应用程序。

您可以从类似 How to Use a Batch File to Create a Command Prompt Menu to Execute Commands 的内容开始并在调用每个应用程序之前添加适当的 set 命令。

ECHO OFF
CLS
:MENU
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Open Notepad
ECHO 2 - Open Calculator
ECHO 3 - Open Notepad AND Calculator
ECHO 4 - EXIT
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO NOTE
IF %M%==2 GOTO CALC
IF %M%==3 GOTO BOTH
IF %M%==4 GOTO EOF
:NOTE
cd %windir%\system32\notepad.exe
start notepad.exe
GOTO MENU
:CALC
cd %windir%\system32\calc.exe
start calc.exe
GOTO MENU
:BOTH
cd %windir%\system32\notepad.exe
start notepad.exe
cd %windir%\system32\calc.exe
start calc.exe
GOTO MENU

来源Display, set, or remove CMD environment variables

Permanent changes

Changes made using the SET command are NOT permanent, they apply to the current CMD prompt only and remain only until the CMD window is closed. To permanently change a variable at the command line use SetX or with the GUI - Control Panel | System | Environment | System/User Variables

Changing a variable permanently with SetX will not affect any CMD prompt that is already open. Only new CMD prompts will get the new setting.

关于windows - 使用批处理脚本在当前 cmd 中设置环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740461/

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