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