zephirus
zephirus

Reputation: 391

How can I iterate in a list in batch?

I have two strings:

jobID: w,x,y,z
Test: A,B,C,D

I'm iterating the second string (after separating it by comma) and print both the value of that generated list and the one from the first string.

My code until now is this:

@echo off
setlocal EnableDelayedExpansion

set num=-1
for %%a in ("%jobID:,=" "%") do (
   set /A num+=1
   set elem[!num!]=%%a
)

set i=-1
for %%a in ("%Test:,=" "%") do (
   set /A i=i+1
   echo file name is %%a and first list element is !%elem[!%i%!]%!
)

What I'm expecting is this:

file name is A and first list element is w
file name is B and first list element is x
file name is C and first list element is y
file name is D and first list element is z

What I get:

file name is "A" and first list element is "0"
file name is "B" and first list element is "1"
file name is "C" and first list element is "2"
file name is "D" and first list element is "3"

EDIT: I need to get the values without the double quotes.

Upvotes: 1

Views: 2983

Answers (1)

Squashman
Squashman

Reputation: 14290

You essentially need to get two phases of variable expansion. This is accomplished by using the CALL command.

Change your echo command to this.

call echo file name is %%a and first list element is %%elem[!i!]%%

This is all explained in Arrays, linked lists and other data structures in cmd.exe (batch) script

Quotes are removed from a FOR variable by using the command modifiers which are explained in the last section of the help file for the FOR command.

You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

Upvotes: 6

Related Questions