Reputation: 33036
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.
Upvotes: 10
Views: 7952
Reputation: 381
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
Upvotes: 0
Reputation: 567
Exactly repeat indents
set arr=(^
"first name"^
"second name"^
)
Check data
for %%a in %arr% do (
echo %%a
)
Upvotes: 3
Reputation: 67216
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
)
Upvotes: 10