gpt4 book ai didi

batch-file - SETLOCAL ENABLEDELAYEDEXPANSION导致CD和PUSHD无法持久保存

转载 作者:行者123 更新时间:2023-12-04 13:49:57 24 4
gpt4 key购买 nike

我正在尝试在批处理脚本中一起使用setlocal enabledelayedexpansioncd,这似乎无法将更改持久化回shell。

我需要setlocal enabledelayedexpansion的原因是,我需要在脚本运行时动态扩展脚本中的变量。

考虑下面的示例批处理文件:

a.bat
================================
setlocal enabledelayedexpansion
cd ..

上面的批处理文件未按预期迁移到先前的目录!

检查 this

最佳答案

问题在于setlocal导致任何当前目录更改都位于批处理文件的本地。

参见setlocal /?:

Begins localization of environment changes in a batch file. Environment changes made after SETLOCAL has been issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings. When the end of a batch script is reached, an implied ENDLOCAL is executed for any outstanding SETLOCAL commands issued by that batch script.



当前目录包含在“环境更改”中。

尝试此操作,请注意它在批处理中回显 C:\%CD%,但是在退出批处理时仍会重置当前目录。
[11:42:00.17] C:\temp
> cat test.bat
@echo off
setlocal enabledelayedexpansion
cd ..
echo %CD%
[11:42:19.38] C:\temp
> test.bat
C:\

[11:42:23.00] C:\temp
>

关于batch-file - SETLOCAL ENABLEDELAYEDEXPANSION导致CD和PUSHD无法持久保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26246151/

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