user2533789
user2533789

Reputation: 1250

Create a list or an array and print each item in Windows Batch

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

Upvotes: 103

Views: 342815

Answers (8)

0Ring
0Ring

Reputation: 1

Here is a 2 function that I created allowing you to create a list I had some problems with the setlocal so I used files I know it's not good but i hope you find it useful

@echo off
rem exemple: set "<name_list>=*list:<items>" & call :funclist "<name_list>"

set "super_list=*list:a b c d" & call :funclist "super_list"
rem OR
set "super_list=*list:a,b,c,d" & call :funclist "super_list"


setlocal EnableDelayedExpansion
for /L %%i in (0, 1, %super_list[len]%) do (
    echo super_list[%%i]: !super_list[%%i]!
)
endlocal
echo -------------------------------------------------
set super_list
pause
exit

:funclist
    set "varlist=%~1"
    set "list_sig=*list:"
    set "var_file=%cd%\%random%%random%.tmp"
    setlocal EnableDelayedExpansion
    if not "!%varlist%:~0,6!" == "%list_sig%" (goto :eof)
    set "list=!%varlist%:%list_sig%=!"

    set /a "index=0"
    for %%a in (!list!) do (
        set "%varlist%[!index!]=%%a"
        set /a "index+=1"
    )

    set /a "index-=1"

    for /L %%i in (0, 1, !index!) do (
        for /f "delims=" %%A in ("!%varlist%[%%i]!") do (
            echo %varlist%[%%i]=%%A>>!var_file!
        )
        echo %varlist%[len]=!index!>>!var_file!
    )
    endlocal

    for /f "delims=" %%a in (%var_file%) do (endlocal & call :global "%%a" "=")

    del /q "%var_file%"

    set "varlist="
    set "list_sig="
    set "var_file="
    goto :eof

:global
    set "global_varval=%~1"
    set "global_split=%~2"

    set "file_var=%cd%\%random%%random%.tmp"
    set "file_val=%cd%\%random%%random%.tmp"

    for /f "tokens=1 delims=%global_split%" %%v in ("%global_varval%") do (echo %%v>%file_var%)
    for /f "tokens=2 delims=%global_split%" %%v in ("%global_varval%") do (echo %%v>%file_val%)

    <"%file_var%" (set /p "split1=")
    <"%file_val%" (set /p "split2=")


    del /q "%file_var%"
    del /q "%file_val%"

    set "%split1%=%split2%"

    set "file_var="
    set "file_val="
    set "global_varval="
    set "global_split="
    set "split1="
    set "split2="
    goto :eof

Upvotes: 0

Odys
Odys

Reputation: 9090

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]!
)

Upvotes: 5

austin-schick
austin-schick

Reputation: 1245

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

Upvotes: 5

user5542121
user5542121

Reputation: 1052

I like this way:

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


for %%a in (%list%) do ( 
 echo %%a
 echo/
)

Upvotes: 5

John Zhu
John Zhu

Reputation: 1099

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/
)

Upvotes: 58

tux
tux

Reputation: 21

@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%]!

Upvotes: 0

Aacini
Aacini

Reputation: 67216

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

Upvotes: 145

Magoo
Magoo

Reputation: 80013

@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.

Upvotes: 21

Related Questions