I have the following batch script
@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
The printing of MY_VAR always is blank. Why is that?
If the img directory contains 2 png files: - img1.png - img2.png
then here is the output from the console:
E:\img>test.cmd
TESTING E:\img\img1.png
TESTING
TESTING E:\img\img2.png
TESTING
Thanks
I corrected the script to use delayed expansion.
@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
The output is now correct.
E:\img>test.cmd
TESTING E:\img\img1.png
TESTING E:\img\img1.png
TESTING E:\img\img2.png
TESTING E:\img\img2.png
Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With