New
#1
Delayed expansion and arrays in Win 10 batch files
I've been researching this one for hours, but can't make any headway...
I have a folder with 3 files in it that looks like this:
ISO Images
> Image 1.iso
> Image 2.iso
> Image 3.iso
I have a batch file that is doing some processing of these ISO images but I'm having a really hard time figuring out some behaviors.
Below is a sample of code showing where my quandary is. I expect the FOR loop to place the full path and filename of each file into an array called SourceFile[x], with x being a number from 1 to 3.
I have a few echo statements in the FOR loop that show me that all values are exactly what I expect and that everything within the FOR loop is working fine.
However, after I drop out of the FOR loop, the variables SourceFile[x] do not seem to hold their values any longer.
Here a sample clip of some code and what actually happens:
Note: I run this batch file as admin
The code clip:
________________
@echo off
setlocal enabledelayedexpansion
set SourceDir="D:\ISO Images"
set /a count=0
for %%a in (!SourceDir!\*.iso) do (
set /a count=count+1
set "SourceFile[%count%]=%%a"
echo The value of count is: !count!
echo The value of a is: %%a
echo The value of SourceFile[!count!] is: !SourceFile[%count%]!
echo.
)
echo.
echo.
echo The current value of count is: %count%
echo %SourceFile[%count%]%
echo %SourceFile[1]%
________________
Here is the resulting output:
_____________________
The value of count is: 1
The value of a is: D:\ISO Images\Image 1.ISO
The value of SourceFile[1] is: D:\ISO Images\Image 1.ISO
The value of count is: 2
The value of a is: D:\ISO Images\Image 2.ISO
The value of SourceFile[2] is: D:\ISO Images\Image 2.ISO
The value of count is: 3
The value of a is: D:\ISO Images\Image 3.ISO
The value of SourceFile[3] is: D:\ISO Images\Image 3.ISO
The current value of count is: 3
count
ECHO is off.
Press any key to continue . . .
__________________
So there are a couple of questions:
1) Why is my array (SourceFile[x]) not holding values after I come out of the FOR loop?
2)This question requires a little explanation...
First, I need to explain my understanding of delayed expansion to make sure I have this correct. My understanding is that within a block (within the parenthesis of a "for" or "if" structure, for example, or on a single line of code), variables are expanded, or assigned their values, before the code is actually executed. My understanding is that with delayed expansion enabled, the variables are not expanded (assigned values) until the last possible moment, in other words, when the code executes and actually references the variable. To do this, the variable has to be referenced like this: !MyVariable!, and not like this: %MyVariable%.
From my testing it seems that setting the value of a variable with a "set" does not need to use the ! rather than the %. For example, within the FOR loop, I need to use
set "SourceFile[%count%]=%%a"
NOT
set "SourceFile[!count!]=%%a"
So with all that said, in the line that reads
echo The value of SourceFile[!count!] is: !SourceFile[%count%]!
why do I need to reference the variable at the end of that line as !SourceFile[%count%]! and not as !SourceFile[!count!]!
If I reference it as !SourceFile[!count!]! then my output reads
The value of SourceFile[3] is: count
rather than
The value of SourceFile[3] is: D:\ISO Images\Image 3.ISO
So to summarize, there is question regarding the syntax for delayed expansion and the question about why my array variables are losing their values after coming out of the FOR loop.