Combining multiple text files into one

You may read several input files in a Batch program via the standard handles. Remember that 0 is Stdin, 1 is Stdout and 2 is Stderr, but this leaves handles 3 to 9 available! The Batch file below do a file merge with the contents of two files; of course, up to 8 files can be combined with this method.

@echo off
setlocal EnableDelayedExpansion
Rem First file is read with FOR /F command
Rem Second file is read via standard handle 3
3< file2.txt (for /F "delims=" %%a in (file1.txt) do (
   Rem Read next line from file2.txt
   set /P line2=<&3
   Rem Echo lines of both files
   echo %%a,!line2!
))

Further details here: http://www.dostips.com/forum/viewtopic.php?f=3&t=3126

Leave a Comment