2

I have a situation where I need to combine four CSV files into one CSV file. This is easy if i just wanted to add them one after the other, but I need to get them to be side by side in the CSV file. I know that all four files have the same number of entries (in the 1000 entry range). I have been working on the following code which works for small files but is extremely inefficient for long files. Is there any easier way to accomplish the same task?

@echo off
setlocal enabledelayedexpansion
Set R=1
Set T=1
Set Y=1
Set U=1
for /f %%a in (test1.txt) do (
    set I=!R!
    for /f %%b in (test2.txt) do (
        set J=!T!
        for /f %%c in (test3.txt) do (
            set K=!Y!
            for /f %%d in (test4.txt) do (
                set L=!U!
                If !I!==!J! If !J!==!K! If !K!==!L! echo %%a,%%b,%%c,%%d >> TestComplete.txt
                Set /a U=U+1
            ) 
            Set U=1
            Set /a Y=Y+1
        )
        Set Y=1
        Set /a T=T+1
    )
    Set T=1
    Set /a R=R+1
)

Note: I know that the code I have pasted in is using .txt files and not .csv files. I assume what will work for one will work for the other.

Again, The above code seems to work great as long as the files are small. I have trouble (of course) when the files (in this case test1.txt) have around 1000 lines of text.

David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • Do the files have a header? If not you can simply use the copy command to combine them. –  Dec 21 '12 at 18:04
  • @a_horse_with_no_name: That would do "union all". But they want a kind of "join on rownum". – Andriy M Dec 21 '12 at 19:08

1 Answers1

2

GnuWin, a collection of ports of GNU (Unix like) tools to Windows, contains paste which does exactly what you want. However, I sense you'd prefer a native Windows answer and I've come up with one, but it's not pretty. Starting with @walid2mi's clever solution and striping it down to coljoin.bat:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%1) do (
    set /p line=
    echo !line!, %%a
)

You can join four files with:

type a.txt | coljoin b.txt | coljoin c.txt | coljoin d.txt > TestComplete.txt

Time required should increase linearly with file length, avoiding the drastic slowdown of your solution. However the failure mode of this solution is not good when all the files do not have an equal number of lines. The user will usually be prompted to enter missing lines.

Community
  • 1
  • 1
jimhark
  • 4,938
  • 2
  • 27
  • 28