0

Hello i want to merge 3 text files line by line using batch file. The lines contains specials char. and spaces. if file has only one line the code below works but if we have multiline text ti doesnt Because the resault is:

1stline1stline 1stline2stline 2stline1stline 2stline2stline Example code

@echo off
for /f "delims=" %%a in (a.txt) do (
    for /f "delims=" %%b in (b.txt) do (
        >>c.txt echo %%a %%b
    )
)
Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
  • Your script iterates all `b-file` lines for each `a-file` line such that output is of `a-size × b-size` (measured in units of _lines_, not in bytes). Windows batch script is not too feasible for such task solving. However, if you would like to stay on it for any reason, you could add two line counters, one counter for each file and `echo` only `if a-counter = b-counter`. But this will work for files of the same "size", so you need find a trick for `a-size < b-size` and for `a-size > b-size` as well... – JosefZ Nov 20 '14 at 16:15
  • Thank you very much it is helpful and I understand it very well! – Dimis Mitrou Nov 20 '14 at 18:05
  • Please, post a small example of the 3 files and the desired output (modify the question, don't use a comment). What happens when the files have not the same number of lines? May the output be placed in any order (a-b or b-a)? – Aacini Nov 20 '14 at 18:08
  • @Aacini: you can generate those files in tested script as follows: `if not exist filea.txt (for /L %%G in (1,1,3) do @echo a file %%G >> filea.txt)` and analogously `... do @echo b file %%G >> fileb.txt)` – JosefZ Nov 20 '14 at 19:39
  • @JosefZ: Excuse me, but this does not answer anyone of my questions... `:(` – Aacini Nov 20 '14 at 19:45
  • @Aacini: not considered an answer, a comment only. A hint how-to create such test files most painlessly :-) – JosefZ Nov 20 '14 at 19:49

1 Answers1

3

I just copied the answer from this site and extended it to 3 files. I can't provide a more precise solution without further details...

@echo off
setlocal EnableDelayedExpansion
rem First file is read with FOR /F command
rem Second file is read via standard handle 3
rem Third file is read via standard handle 4
3< b.txt 4< c.txt (for /F "delims=" %%a in (a.txt) do (
   rem Read next line from b.txt
   set /P lineB=<&3
   rem Read next line from c.txt
   set /P lineC=<&4
   rem Echo lines of three files
   echo %%a,!lineB!,!lineC!
))
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108