1

I need to loop through a file, affect the values to a variable, then loop into a second file and use these previous variables:

for /F "delims=, tokens=1-3" %%a in (Test_1.txt) do (
    set Var1=%%b
    set Var2=%%c
    for /F "delims=, tokens=1-4" %%a in (Test_2.txt) do (
        if "%%d"=="" (
            echo %%a           !var1!            !Var2!>>Test_3.txt
        )
    )
)

How can I affect the first parameters to a variable?

4
  • 2
    The inner FOR should not use %%a as loop variable like the outer FOR using %%a, %%b and %%c for the three tokens, but for example %%d and so %%d, %%e, %%f and %%g for the four tokens. PS: I recommend to use upper case loop variables instead of lower case. Run in a command prompt window for /? to get displayed the help on several pages and read especially the section about the modifiers and you should know why the usage of %%A to %%G would be better as less confusing. Commented Aug 29, 2018 at 9:41
  • Fixing the issue is easy, but you do realise this will not match line by line right? each of the lines in file1 will be matched with each of the lines in file 2.. not line 1 matched to line 1 only.. Commented Aug 29, 2018 at 10:11
  • Your question is not quite clear. Do you want to read your files in parallel line by line? If so, two nested for /F loops are not the right choice... Commented Aug 29, 2018 at 10:18
  • No, I want to check the first line of Test_1 in all Test_2 lines.Then the second line of Test_1 in all Test_2 lines etc. Commented Aug 29, 2018 at 10:23

1 Answer 1

1

Based on your comment, you then do not even need delayed expansion at all:

Each loop must have it's own unique token variables.

for /F "delims=, tokens=1-3" %%a in (Test_1.txt) do (
    for /F "delims=, tokens=1-4" %%d in (Test_2.txt) do (
        if "%%e"=="" (
            echo %%d           %%a           %%b >> Test_3.txt
        )
    )
)
Sign up to request clarification or add additional context in comments.

Comments

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.