0

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
2
  • It looks like you're trying to map an ID to a file name. If that's the case, you should use arrays instead. Commented Aug 19, 2019 at 7:34
  • 2
    Well, you seem to misunderstand the concept of nested (for) loops. 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... Commented Aug 19, 2019 at 7:44

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

what if Id value is not 1,2 instead its some random number. example, Id=4,8 and I want to display Id= 4 and filename = test1 Id= 8 and filename = test2
Well, what about set /A "Id+=4" then?
Thanks but this approach have an additional step of maintaining Id's value at 2 places in the code. At start Set "Ids=4,8" and then set /A "Id+=4" Moreover, like I said Id=4,8 is just an example, Id value could any numeric value in any pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.