Ron
Ron

Reputation: 2503

how to iterate over two lists in batch processing

I would like to run a console app from a batch file. actually, the console app is run from another batch file. The app can take two command line parameters, a and b. They are file names. I have two lists of files, alist.txt and blist.txt, which are the same length. I would like, if possible to run the batch like this:

FOR /F %%a in (alist.txt)  %%b in (blist.txt) do runconsolebatch.bat %%a %%b

but that doesn't work. When I run it I get "%b was unexpected at this time."

If I somehow nest them, that wouldn't work. I could join the two file lists into one file...like this?

  file1A    file2A

  file1B    file2B

but I'm not sure how I could assign file1A to %a and file1B to %b, or if that's possible.

4/12/2015: I thought this was solved. I even used what I thought was the correct batch file but it doesn't work. What I get now is the first output line is correct and the rest like this:

The first line works correctly. BUT I only get two more lines even though the DIR.TXT file has 5 entries and the NAMES.TXT file has 3 ENTRIES. That should be 15 lines of output. The rest of the output is this:

G:\test>(
set /p "valueA="  
 runconsolebatch.bat !valueA! name1.txt 
)  

Command line: in=!valueA! name1.txt 

failure validating G:\test\source\!VALUEA!: file may not be correctly specified or doesn't exist. 

This is the batch file I'm using:

@echo off
    setlocal enableextensions enabledelayedexpansion
    <"dir2.txt" (
        for /f "usebackq delims=" %%b in ("names.txt") do (
            set /p "valueA="
            runconsolebatch.bat !valueA! %%b
        )
    )

And if I change runconsolebatch.bat to echo so I get

echo !valueA!  %%b

I get three lines of output: the first line from dir2.txt and the first line from names.txt, second and second and third and third.

FOR SOME REASON, while this worked at one time, it doesn't now. Here's my batch file:

@echo off
setlocal enableextensions enabledelayedexpansion
<"blist.txt" (
    for /f "usebackq delims=" %%b in ("alist.txt") do (
        set /p "valueA="
        echo !valueA! %%b
    )
)

blist.txt is

1111
2222
3333
4444

and alist.txt is

AAAA
BBBB
CCCC

but the output of the batch file is:

1111 AAAA
2222 BBBB
3333 CCCC

when it should be

1111 AAAA
1111 BBBB

etc.

Doubtless it's a silly typo but...what? Also this is just run in a regular user command shell.

Upvotes: 2

Views: 1424

Answers (1)

MC ND
MC ND

Reputation: 70923

Read the Blist.txt with a for loop and AList.txt with a file input redirection and set /p command.

@echo off
    setlocal enableextensions enabledelayedexpansion
    <"Alist.txt" (
        for /f "usebackq delims=" %%b in ("Blist.txt") do (
            set /p "valueA="
            echo !valueA! %%b
        )
    )

For values as the indicated in the question this should work. Problematic characters or lines longer than 1021 characters should need aditional code.

Upvotes: 5

Related Questions