Search code examples
arrayswindowsbatch-file

Create an array in batch in multiple lines


I am trying to copy a list of files to a specific directory using a batch file. The first thing I need to do is to create a list of file names. I saw this this post Create list or arrays in Windows Batch. The following works fine. But I am not happy about the fact that it is in one line. As my list of files gets bigger and bigger, it becomes hard to read.

set FILE_LIST=( "file1.txt" "file2.txt" "file3.txt" )

And then I noticed this blog. It creates an array with multiple lines.

set FILE_LIST[0]="file1.txt"
set FILE_LIST[1]="file2.txt"
set FILE_LIST[2]="file3.txt"

I am wondering whether there is a way of creating a array as the following:

set FILE_LIST=( "file1.txt" 
     "file2.txt" 
     "file3.txt" )

so that I can separate the file names into multiple lines, while do not need to worry about the index.


Solution

  • In the same topic you refer to there is the equivalent of this solution (below "You may also create an array this way"):

    setlocal EnableDelayedExpansion
    set n=0
    for %%a in ("file1.txt"
                "file2.txt"
                "file3.txt"
               ) do (
       set FILE_LIST[!n!]=%%a
       set /A n+=1
    )