为什么setlocal会干扰Windows批处理文件中的chdir? [英] Why setlocal interferes with chdir in windows batch files?

查看:99
本文介绍了为什么setlocal会干扰Windows批处理文件中的chdir?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行批处理文件

setlocal
chdir ..

目录未更改,但是如果我运行

the directory is not changed, but if I run

setlocal
endlocal 
chdir ..

它正常工作.这必须与setlocal完全相同.但是,当您阅读setlocal的定义(与如何查看环境变量有关)时,并不是很明显.我希望这是一个很好的机会来解释setlocal的实际作用以及它为什么会干扰chdir.

it works normally. This must be exactly what is expected with setlocal. However, it is not entirely obvious when you read the definition of setlocal, which is related to how the environment variables are seen. I am hoping that this is a good occasion to explain what setlocal actually does and why it interferes with chdir.

推荐答案

SETLOCAL(help setlocalsetlocal /?)的HELP文档实际上很好地解释了这种情况.唯一不明显的是" 环境变化的本地化 "不仅包含环境变量,还包含当前目录以及延迟的扩展和扩展名状态.可能还有更多,但我暂时无法想到.

The HELP documentation for SETLOCAL (help setlocal or setlocal /?) actually explains the situation pretty well. The only thing that is not obvious is that "localization of environment changes" doesn't just include environment variables, but also includes the current directory, and the delayed expansion and extensions states. There may be more, but I can't think of it at the moment.

绊倒您的事情实际上得到了很好的解释:" 当到达批处理脚本的末尾时,对于该批处理脚本发出的所有未完成的SETLOCAL命令,将执行隐含的ENDLOCAL./em> ".未说明的是,被调用子例程也是如此.

The thing that is tripping you up is actually explained fairly well: "When the end of a batch script is reached, an implied ENDLOCAL is executed for any outstanding SETLOCAL commands issued by that batch script." Not stated is that the same is true for called subroutines.

批处理脚本结束时,隐式ENDLOCAL会擦除" CHDIR的效果.在第二个代码中显式的ENDLOCAL使您返回到根环境,因此将保留CHDIR.

When your batch script ends, the implicit ENDLOCAL "erases" the effect of your CHDIR. Your explicit ENDLOCAL in your second code gets you back to the root environment, so your CHDIR is then preserved.

更新

即使您通常可以使用%CD%获取当前值,当前目录也不是环境变量.您可以通过调整SET CD来证明这一点-它可能会给您未定义环境变量CD".如果使用set "CD=some value"显式定义自己的真实CD变量,则%CD%将返回您分配的值,而不是当前目录.

The current directory is not an environment variable, even though you can normally get the current value using %CD%. You can prove it by tring SET CD - it will probably give you "Environment variable CD not defined". If you explicitly define your own true CD variable using set "CD=some value", then %CD% will return the value you assigned, and not the current directory.

原始的SETLOCAL命令不能控制COMMAND.COM以前的延迟扩展或扩展.引入CMD.EXE时,添加了启用/禁用DelayedExpansion"和启用/禁用扩展名"选项.这就是MS决定实施此功能的方式.不一定要那样.在许多方面,不幸的是,如果没有SETLOCAL/ENDLOCAL,您将无法控制这些状态.我经常希望我可以在不本地化环境的情况下启用或禁用延迟扩展.

The original SETLOCAL command did not control delayed expansion or extensions back in the old COMMAND.COM days. The Enable/Disable DelayedExpansion and Enable/Disable Extensions options were added when CMD.EXE was introduced. It's just how MS decided to implement the feature. It didn't have to be that way. In many ways it is unfortunate that you cannot control those states without SETLOCAL/ENDLOCAL. I often wish I could enable or disable delayed expansion without localizing the environment.

这篇关于为什么setlocal会干扰Windows批处理文件中的chdir?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆