for循环中未设置的批处理脚本变量无效 [英] batch script variable unset in for loop has no effect
问题描述
下面是我的脚本.我正在尝试查看下面一级的文件夹,并仅选择那些文件夹,因此〜-9从路径中提取了最后9个字符.但是set var =不会取消设置变量,因为输出将返回相同的文件夹名称,重复#次.另外,批处理不允许我直接在%% i上执行此提取技巧,因此需要局部变量.
Below is my script. I am trying to look into folders one level below and pick out only those folders, hence the ~-9 which extracts the last 9 chars from the path. But the set var= does not unset the variable because the output comes back with the same folder name repeated # times. Also batch doesn't allow me to do this extract trick directly on %%i, hence the need for the local variable.
如何清除此变量,以便在下一次迭代中采用新值?
How do I clear this variable so that it takes the new value in the next iteration?
@echo off
for /d %%i in (%1\*) do (
set var=%%i
echo %var:~-9%
set "var="
)
推荐答案
http://judago.webs. com/variablecatches.htm 解释了我的问题.魔术线是setlocal enabledelayedexpansion,并将var称为echo!var:〜-9!. ! vs%...哇! cmd仍然令我惊讶.
http://judago.webs.com/variablecatches.htm has an explanation for my problem. The magic lines were setlocal enabledelayedexpansion and calling var as echo !var:~-9!. ! vs % ...wow! cmd still amazes me.
这篇关于for循环中未设置的批处理脚本变量无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!