98

Can I declare a list or array in a batch file like this:

set list = "A B C D"

And then I need to write these to a file, with the spaces between:

A

B

C 

D
YakovL
  • 7,557
  • 12
  • 62
  • 102
user2533789
  • 1,192
  • 3
  • 9
  • 14

7 Answers7

141

Yes, you may use both ways. If you just want to separate the elements and show they in separated lines, a list is simpler:

set list=A B C D

A list of values separated by space may be easily processed by for command:

(for %%a in (%list%) do (
   echo %%a
   echo/
)) > theFile.txt

You may also create an array this way:

setlocal EnableDelayedExpansion
set n=0
for %%a in (A B C D) do (
   set vector[!n!]=%%a
   set /A n+=1
)

and show the array elements this way:

(for /L %%i in (0,1,3) do (
   echo !vector[%%i]!
   echo/
)) > theFile.txt

For further details about array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

ATTENTION! You must know that all characters included in set command are inserted in the variable name (at left of equal sign), or in the variable value. For example, this command:

set list = "A B C D"

create a variable called list (list-space) with the value "A B C D" (space, quote, A, etc). For this reason, it is a good idea to never insert spaces in set commands. If you need to enclose the value in quotes, you must enclose both the variable name and its value:

set "list=A B C D"

PS - You should NOT use ECHO. in order to left blank lines! An alternative is ECHO/. For further details about this point, see: http://www.dostips.com/forum/viewtopic.php?f=3&t=774

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Yeah... that vector[i] is what I was looking for! Thanks! – JasonXA May 26 '17 at 12:04
  • 3
    And if you use `for /L %%i in (0,1,%n%)` you should get them all, instead of just the first 3. :) – Jesse Chisholm Feb 13 '18 at 18:57
  • I found in my case that, n is not retained outside of the loop, using '3' overshoots the vector by 1, so "0,1,2" seems correct and using the simple "set list=a b c" didn't work with more complex data like *.dll. I wanted a list of file extensions. – bduhbya Sep 30 '19 at 23:38
  • @JesseChisholm: Not directly. You need to do a `set /A n-=1` command before the second `for`. For this reason is better to create the array as base-1 (from 1 to n, not from 0 to n-1). – Aacini Oct 01 '19 at 12:59
50

Sometimes the array element may be very long, at that time you can create an array in this way:

set list=a
set list=%list%;b 
set list=%list%;c 
set list=%list%;d

Then show it:

@echo off
for %%a in (%list%) do ( 
 echo %%a
 echo/
)
John Zhu
  • 1,009
  • 11
  • 11
  • 1
    This is the real right awser IMO. This is really what I was looking for, seems to be the closest to other langs and hence what I expected to see.. Thx – Mike Q Mar 05 '20 at 17:36
21
@echo off
setlocal
set "list=a b c d"
(
 for %%i in (%list%) do (
  echo(%%i
  echo(
 )
)>file.txt

You don't need - actually, can't "declare" variables in batch. Assigning a value to a variable creates it, and assigning an empty string deletes it. Any variable name that doesn't have an assigned value HAS a value of an empty string. ALL variables are strings - WITHOUT exception. There ARE operations that appear to perform (integer) mathematical functions, but they operate by converting back and forth from strings.

Batch is sensitive to spaces in variable names, so your assignment as posted would assign the string "A B C D" - including the quotes, to the variable "list " - NOT including the quotes, but including the space. The syntax set "var=string" is used to assign the value string to var whereas set var=string will do the same thing. Almost. In the first case, any stray trailing spaces after the closing quote are EXCLUDED from the value assigned, in the second, they are INCLUDED. Spaces are a little hard to see when printed.

ECHO echoes strings. Clasically, it is followed by a space - one of the default separators used by batch (the others are TAB, COMMA, SEMICOLON - any of these do just as well BUT TABS often get transformed to a space-squence by text-editors and the others have grown quirks of their own over the years.) Other characters following the O in ECHO have been found to do precisely what the documented SPACE should do. DOT is common. Open-parenthesis ( is probably the most useful since the command

ECHO.%emptyvalue%

will produce a report of the ECHO state (ECHO is on/off) whereas

ECHO(%emptyvalue%

will produce an empty line.

The problem with ECHO( is that the result "looks" unbalanced.

Magoo
  • 77,302
  • 8
  • 62
  • 84
5

Array type does not exist

There is no 'array' type in batch files, which is both an upside and a downside at times, but there are workarounds.

Here's a link that offers a few suggestions for creating a system for yourself similar to an array in a batch: http://hypftier.de/en/batch-tricks-arrays.

  • As for echoing to a file echo variable >> filepath works for echoing the contents of a variable to a file,
  • and echo. (the period is not a typo) works for echoing a newline character.

I think that these two together should work to accomplish what you need.

Further reading

zicameau
  • 139
  • 1
  • 11
austin-schick
  • 1,225
  • 7
  • 11
5

I like this way:

set list=a;^
b;^
c;^ 
d;


for %%a in (%list%) do ( 
 echo %%a
 echo/
)
user5542121
  • 1,051
  • 12
  • 28
  • 1
    If you are wondering what the caret (`^`) does, it's to allow a single command to extend across different lines. See [Split long commands in multiple lines through Windows batch file](https://stackoverflow.com/questions/69068/split-long-commands-in-multiple-lines-through-windows-batch-file) for more info. – Fabio says Reinstate Monica Jun 16 '23 at 14:45
4

Set up environment

@echo off
setlocal enabledelayedexpansion

Create a list

set items[0]=First
set items[1]=Second
set items[2]=Third
set items[3]=Fourth
set items[4]=Fifth

Select from the list

%items[1]%

Display item from the list

echo %items[1]%

Ask user for a choice (the key in the list) and return the corresponding item

set /p choice="Enter choice:
echo !items[%choice%]!

Display the list. The magic numbers in the parenthesis are: 0 the starting index of the array. 1 the step. 4 the last item. %%i is your index.

for /L %%i in (0,1,4) do (
    echo !items[%%i]!
)
Odys
  • 8,951
  • 10
  • 69
  • 111
0
@echo off

set array=

setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set nl=^&echo(

set array=auto blue ^!nl!^
  bycicle green ^!nl!^
  buggy   red

echo convert the String in indexed arrays

set /a index=0

for /F "tokens=1,2,3*" %%a in ( 'echo(!array!' ) do (

 echo(vehicle[!index!]=%%a color[!index!]=%%b 
 set vehicle[!index!]=%%a
 set color[!index!]=%%b
 set /a index=!index!+1   

)

echo use the arrays

echo(%vehicle[1]% %color[1]%
echo oder

set index=1
echo(!vehicle[%index%]! !color[%index%]!
tux
  • 21
  • 1