The answer of walid2me is awesome, this is only a small mofication to show how to make it safe against characters like !^
in the file 1.txt
, the content of file 2.txt
is safe, a it is read with teh set/p
syntax.
@echo off
set f1=1.txt
set f2=2.txt
set "sep= " % tab %
(
setlocal DisableDelayedExpansion
for /f "delims=" %%a in (%f1%) do (
set "f1_line=%%a"
setlocal EnableDelayedExpansion
set /p f2_line=
echo(!f1_line!!sep!!f2_line!
endlocal
)
endlocal
)<%f2%
pause
goto :eof
As you can see, I only move the expansion of %%a
just before the setlocal EnableDelayedExpansion
There is still another small flaw, as set/p
strips trailing control characters, like single CR, LF and also TAB.
This affects only the file 2.txt.
In depth analysis about set /p
are at New technic: set /p can read multiple lines from a file