0

I have a File_2 that contains lines as follow

A1,A2,A3,A4
B1,B2,B3,B4

In another File_1 I have multiple lines as follow:

X
Y
Z

What I Want is to have File_3 with all possible combinations:

A1;A2;X;A4
A1;A2;Y;A4
A1;A2;Z;A4
B1;B2;X;B4
B1;B2;Y;B4
B1;B2;Z;B4

I use a code that populate an array from File_1 then I try to combine it with File_2 to obtain the File_3:

SET /A i=0
FOR /F "tokens=1" %%a IN (File_1.txt) DO (
    SET /P Var[%i%]=%%a
    SET /A i=+1
)

SET /A Counter=0

FOR /F "delims=, tokens=1-7" %%a IN (File_2.txt) DO (
    IF %Counter% LEQ %i% (
        ECHO %%a;%%b;%%Var[i]%%;%%d;>>File_3.txt
        SET /A Counter=+1
    )
)

The second loop doesn't seem to work. How can I use my Array's values, knowing that my Array is not static?

1
  • %Counter% does not update, you need to apply delayed expansion, so use !Counter!. But why not using for /L %%I in (1,1,%Counter%) do call echo %%a;%%b;%%Var[%%I]%%;%%d>>File_3.txt? Commented Aug 16, 2018 at 11:40

1 Answer 1

2

no need for an array; just two nested for /f loops:

@echo off
(for /f %%x in (file_1.txt) do (
  for /f "tokens=1-4 delims=," %%a in (file_2.txt) do (
    echo %%a;%%b;%%x;%%d
  )
))>file_3.txt

Output:

A1;A2;X;A4
B1;B2;X;B4
A1;A2;Y;A4
B1;B2;Y;B4
A1;A2;Z;A4
B1;B2;Z;B4

or

@echo off
(for /f "tokens=1-4 delims=," %%a in (file_2.txt) do (
  for /f %%x in (file_1.txt) do (
    echo %%a;%%b;%%x;%%d
  )
))>file_3.txt

output:

A1;A2;X;A4
A1;A2;Y;A4
A1;A2;Z;A4
B1;B2;X;B4
B1;B2;Y;B4
B1;B2;Z;B4

depending how you want it sorted.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.