Windows 10: Delayed expansion and arrays in Win 10 batch files

Discus and support Delayed expansion and arrays in Win 10 batch files in Windows 10 Support to solve the problem; 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... Discussion in 'Windows 10 Support' started by hsehestedt, Dec 15, 2018.

  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.

    :)
     
    hsehestedt, Dec 15, 2018
    #1
  2. oily_17 Win User

    Batch Files Knowledge Base

    Don't use batch files very much but one I find useful is for creating a list of files in a directory.
    Just copy the text below into Notepad and save as Filelist.bat

    dir /a /-p /o:gen >filelist.txt

    When you run it from a folder it will create a Filelist.txt file,in the same folder, that lists all the files in that folder.Useful when you are too lazy to type all the files out yourself.
     
    oily_17, Dec 15, 2018
    #2
  3. Batch Files Knowledge Base

    Very nice and usefull thanks for contributing.
     
    AphexDreamer, Dec 15, 2018
    #3
  4. Kreij Win User

    Delayed expansion and arrays in Win 10 batch files

    need batch file help

    Do I sense a little of Therm's evil side? *Big Grin Delayed expansion and arrays in Win 10 batch files :D

    @Sol ...

    Doesn't the Call command delay batch execution until the executable returns ?
    No matter how long that is ? Or do you need to wait for a set period after the executable returns?

    This is how I thought it worked ...

    Start batch file
    Call program.exe
    Batch file stops and waits
    program.exe finishes
    Batch file continues again
     
    Kreij, Dec 15, 2018
    #4
Thema:

Delayed expansion and arrays in Win 10 batch files

Loading...
  1. Delayed expansion and arrays in Win 10 batch files - Similar Threads - Delayed expansion arrays

  2. File Explorer expansion for icons

    in Windows 10 Network and Sharing
    File Explorer expansion for icons: HiI am looking for a description for all icons found in File Explorer/Windows Explorer. Do anyone have a such list showing what all the icons mean, please?//marsk...
  3. File Explorer expansion for icons

    in Windows 10 Software and Apps
    File Explorer expansion for icons: HiI am looking for a description for all icons found in File Explorer/Windows Explorer. Do anyone have a such list showing what all the icons mean, please?//marsk...
  4. Expansion of files on copying

    in Windows 10 Software and Apps
    Expansion of files on copying: I have a 5TB disk E: which was just about full of archive images.So I bought an 8TB disk H: and copied ca 4.3TB from E to H:But according to H properties, there are now 5.93TB stored on it.The copying process has added double arrows, i.e. apparently compressing the...
  5. Expansion of files on copying

    in Windows 10 Network and Sharing
    Expansion of files on copying: I have a 5TB disk E: which was just about full of archive images.So I bought an 8TB disk H: and copied ca 4.3TB from E to H:But according to H properties, there are now 5.93TB stored on it.The copying process has added double arrows, i.e. apparently compressing the...
  6. Expansion of files on copying

    in Windows 10 Gaming
    Expansion of files on copying: I have a 5TB disk E: which was just about full of archive images.So I bought an 8TB disk H: and copied ca 4.3TB from E to H:But according to H properties, there are now 5.93TB stored on it.The copying process has added double arrows, i.e. apparently compressing the...
  7. Batch Files

    in Windows 10 Software and Apps
    Batch Files: How do i make it that my batch file puts itself in to startup https://answers.microsoft.com/en-us/windows/forum/all/batch-files/6156404d-20d4-4f58-bee5-14629dbf7b10
  8. Create batch files with Batch?

    in Windows 10 Software and Apps
    Create batch files with Batch?: How to create Batch and VBS files with Batch? So I write a batch file which I then convert to exe that creates new folders in a certain directory and then creates batch and VBS files with a certain content and saves them in the created folder. So it's similar to a program...
  9. Shutting Down Win 10 Remotely batch file?

    in Windows 10 Ask Insider
    Shutting Down Win 10 Remotely batch file?: Hello all, I've part resolved this but not fully. I run Kodi on a Win 10 Pro machine and that connects to another Win 10 Pro to access movies etc. I've created a batch script when run on the Kodi box to shutdown the movies box. It seems to work but not if the movie box...
  10. Delete or moving files has a delay. Win 10

    in Windows 10 Performance & Maintenance
    Delete or moving files has a delay. Win 10: Over last few weeks I have been noticing that when I go to delete a file or move it another folder it will actually take around 5-10 seconds to actually execute. I dont mean slow transfer but its like the process hangs for a bit. Weird thing is that it doesn't do it every...

Users found this page by searching for:

  1. windows batch access array expansion