10

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.

3 Answers 3

10

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
)
Sign up to request clarification or add additional context in comments.

3 Comments

why did you use setlocal EnableDelayedExpansion, I've read this answer:stackoverflow.com/a/6680005/4608491 , but I still don't understand why you use it.
@123iamking: The reason is explained in the second topic linked in this question: "You must use !index! to store values in array elements when the index is changed inside a FOR or IF: set elem[!index!]=New value". The setlocal EnableDelayedExpansion command enables the use of !delayed! expansion. The %standard% expansion does not update the variable value inside an IF or FOR command...
@123iamking: Your linked answer just answers this question: "Are the two in fact separate commands and can be written on separate lines?". I suggest you to search and read more questions/answers on Delayed Expansion topic, like the answer below that one that begins this way: "The existing answers don't explain it (sufficiently) IMHO"...
3

Exactly repeat indents

set arr=(^
    "first name"^
    "second name"^
)

Check data

for %%a in %arr% do (
    echo %%a
)

2 Comments

please consider explaining, it didn't work for me.
Typed in *.bat file Setted array of strings and acces to cell of array from for loop iteration Go to console (cmd), then run *.bat file
1

I wanted to expand on Redee's answer because it was better for my purposes than the accepted answer, but it's got a couple typos and it could use an explanation. The ^ character is a line continuation (among other things); it basically says to the interpreter "ignore the following line break character".

So if you want to have a list item every line instead of a gigantic space-delimited list that's impossible to read, the code should look like this:

@echo off

set mylist=^
    hello^
    goodbye^
    bonjour^
    hola^
    adios^
    adieu

for %%a in (%mylist%) do echo %%a

This outputs the following:

hello
goodbye
bonjour
hola
adios
adieu

Note that the number of spaces doesn't matter, as long as it's > 1. This will output the same as above:

@echo off

set mylist=      ^
  hello          ^
         goodbye ^
 bonjour         ^
       hola      ^
adios            ^
    adieu

for %%a in (%mylist%) do echo %%a

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.