我有以下批处理脚本
@echo off
setlocal
set TEST_DIR="E:/img"
:: for all png files in TEST_DIR
for /r "%TEST_DIR%" %%f in (*.png) do (
:: print file full path
echo TESTING %%f
:: store the path into MY_VAR
set MY_VAR=%%f
:: print MY_VAR (always blank!?)
echo TESTING %MY_VAR%
)
endlocal
MY_VAR的打印始终为空白。这是为什么?
如果img目录包含2个png文件: - img1.png - img2.png
然后这是控制台的输出:
E:\img>test.cmd
TESTING E:\img\img1.png
TESTING
TESTING E:\img\img2.png
TESTING
由于
答案 0 :(得分:2)
我更正了脚本以使用延迟扩展。
@echo off
setlocal enableDelayedExpansion
set TEST_DIR="E:/img"
:: for all png files in TEST_DIR
for /r "%TEST_DIR%" %%f in (*.png) do (
:: print file full path
echo TESTING %%f
:: store the path into MY_VAR
set MY_VAR=%%f
:: print MY_VAR
echo TESTING !MY_VAR!
)
endlocal
现在输出正确。
E:\img>test.cmd
TESTING E:\img\img1.png
TESTING E:\img\img1.png
TESTING E:\img\img2.png
TESTING E:\img\img2.png
谢谢。