Vikas J
Vikas J

Reputation: 887

Batch coding nested loop issue

I am trying to iterate values using a but I am not getting expected results. Based on the actual output I think the nested loop is not required here, but I am not sure how can I achieve Expected output without loops.

@Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
Set "Ids=1,2"
Set "basePath=C:\Users\Documents\ReusableQueries\"
Set "fileName=test1,test2"
Set "extension=.csv"
echo ****Configuation Details****
Echo basePath - %basePath%
Echo fileName - %fileName%
Echo extension - %extension%
echo.
For %%i in (%Ids%) do (
    For %%f in (%fileName%) do (
        echo Id - %%i and file name - %%f
        Set "completepath=%basePath%%%f%extension%"
        echo completepath - !completepath!
        FOR /F "tokens=*" %%a IN (!completepath!) do (
            echo Result of api command1 call for Id : %%i and fileName: %%f
            echo.
        )
    )
)
Pause

Current Output:

****Configuation Details****
basePath - C:\Users\Documents\ReusableQueries\
fileName - test1,test2
extension - .csv

Id - 1 and file name - test1
completepath - C:\Users\Documents\ReusableQueries\test1.csv
Result of api command1 call for Id : 1 and fileName: test1

Id - 1 and file name - test2
completepath - C:\Users\Documents\ReusableQueries\test2.csv
Result of api command1 call for Id : 1 and fileName: test2

Id - 2 and file name - test1
completepath - C:\Users\Documents\ReusableQueries\test1.csv
Result of api command1 call for Id : 2 and fileName: test1

Id - 2 and file name - test2
completepath - C:\Users\Documents\ReusableQueries\test2.csv
Result of api command1 call for Id : 2 and fileName: test2

Expected Output:

****Configuation Details****
basePath - C:\Users\Documents\ReusableQueries\
fileName - test1,test2
extension - .csv

Id - 1 and file name - test1
completepath - C:\Users\Documents\ReusableQueries\test1.csv
Result of api command1 call for Id : 1 and fileName: test1

Id - 2 and file name - test2
completepath - C:\Users\Documents\ReusableQueries\test2.csv
Result of api command1 call for Id : 2 and fileName: test2

Upvotes: 0

Views: 84

Answers (2)

sst
sst

Reputation: 1463

Since your are using numeric and consecutive Ids, aschipfl's answer is best suited for your needs.

Here is a more general approach to give you an idea what can be done is the case that the Ids are not numeric or are random.

set NameIdPairs="test1,1", "test2,2", "Filename with spaces,0xfca"
set "basePath=C:\Users\Documents\ReusableQueries\"
set "extension=.csv"

for %%P in (%NameIdPairs%) do (
    echo Current Pair: %%P
    for /F "tokens=1,2 delims=," %%F in (%%P) do (
        echo Id - %%G and file name - %%F
        set "completepath=%basePath%%%F%extension%"
        echo completepath - !completepath!
    )
)

Each pair of filename and id are grouped in the format "filename,id"
There must not be any spaces around the comma(,) which separates them or else they will become part the filename and or id

The pair themselves are separated from each other by comma(,) and/or spaces.

The outer loop picks one pair at each iteration then the inner loop tokenizes the pair to extract the filename and id

Upvotes: 2

aschipfl
aschipfl

Reputation: 34899

You seem to misunderstand the concept of nested (for) loops: they do not loop parallelly, rather the inner loop runs in every iteration of the outer one.

So to get what you want, given that Id is always numeric and consecutive, remove the for %%i loop, do set /A "Id=0" before the for %%f loop, do set /A "Id+=1" as first command in the loop body and use !Id! instead of %%i, like this:

rem // Your code before the loop structure, except `set "Ids=1,2"`...
echo/
set /A "Id=0"
for %%f in (%fileName%) do (
    set /A "Id+=1"
    echo Id - !Id! and file name - %%f
    set "completepath=%basePath%%%f%extension%"
    echo completepath - !completepath!
    for /F "usebackq tokens=*" %%a in ("!completepath!") do (
        echo Result of api command1 call for Id : !Id! and fileName: %%f
        echo/
    )
)
rem // Your code after the loop structure...

Upvotes: 2

Related Questions