1

I want to concatenate the first line of my txt-file with the first line of another txt file. The second with second and so on. Now I have the following code, which concatenates every line with every line. With this code I get all 4 combinations, but I need only 2

for /f "delims=" %%c in (barcode.txt) do (
    for /f "delims=" %%d in (destination.txt) do (
        >>combine.txt echo %%c %%d
    )
)
Oleg_08
  • 447
  • 1
  • 4
  • 23

3 Answers3

3

This does have its limitations but will work for files that the lines lengths are not longer than 1023.

@echo off
setlocal enabledelayedexpansion

< file2.txt (FOR /F "delims=" %%G IN (file1.txt) DO (
    set /p file2=
    echo %%G !file2!
    )
)
pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
2

The following script uses two nested for /F loops, where the inner one is placed in a sub-routine :SUB. A global index variable $INDEX counts the interations of the outer loop, hence it reflects the number of the processed line of the related file. This index is used for the inner loop to define the number of lines of the related text file to skip (skip option of for /F); the loop is then left upon the first iteration, so only a single line is read, namely the one with the same index as the one currently read from the outer loop. This is more complicated and slower than Squashman's method, but it does not mix up different methods for reading files (for /F and input redirection):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set /A "$INDEX=0"

for /F usebackq^ delims^=^ eol^= %%J in ("barcode.txt") do (
    set "LINE=%%J"
    set /A "$INDEX+=1"
    call :SUB TEXT "destination.txt"
    setlocal EnableDelayedExpansion
    echo(!LINE! !TEXT!
    endlocal
)

endlocal
exit /B

:SUB  rtn_line_text  val_file_path
set /A "SKIP=$INDEX-1"
if %SKIP% LEQ 0 (set "SKIP=") else (set "SKIP=skip^=%SKIP%")
for /F usebackq^ %SKIP%^ delims^=^ eol^= %%I in ("%~2") do (
    set "%~1=%%I"
    exit /B
)
exit /B

Redirect (>) the output of the batch file to a text file to get desired combined file.


Here is an alternative approach using input redirection (<) for both files. If the lines of both files are empty, the goto loop breaks and therefore the script terminates:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

4< "barcode.txt" 3< "destination.txt" call :SUB

endlocal
exit /B

:SUB
set "LINE1=" & set "LINE2="
<&4 set /P "LINE1="
<&3 set /P "LINE2="
if not defined LINE1 if not defined LINE2 exit /B
setlocal EnableDelayedExpansion
echo(!LINE1! !LINE2!
endlocal
goto :SUB
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
-1

This happens because you are nesting the for loops. Use just the outer loop. Write a function or routine that returns the nth line from the second file, and use a counter created in the for loop for the first one to retrieve the matching line. While I'm not familiar with batch syntax, this is the general idea:

rem initialize variable i = 1
for /f "delims=" %%c in (barcode.txt) do (
    rem write f(index): returns the line number index from the second file
    rem then concatenate the lines
    rem and then increment i
)
reischapa
  • 41
  • 1
  • 5