3

I want to merge 70,000 RTF and DOC files into one.

I tried to use Microsoft Word 2010 and do "Insert"→"Object"→"Text from file", but it has some limitations as it doesn't stich together more than a couple of dozens of files, no matter how many you select.

Are there any (preferably open source) utilities that can merge such an amount of files reasonably fast?

2
  • I'd say automate the task using VBA or something along the lines. Myself, I'd go with either that or the AutoIt tool which would probably yield a ten-liner quick and dirty script to repeat the manual operation of inserting an object.
    – minya
    May 12, 2012 at 18:16
  • OK, bad news, as I don't possess any particular programming skills :) Could you/someone else help write a macro, executable under Windows 7 64-bit/MS Word 2010? I'm not a noob, but I haven't done any VBA programming myself. Basically the files are located in ONE folder in the format 1.rtf, 2.rtf, ...
    – Andrew
    May 12, 2012 at 18:25

2 Answers 2

3

Here you go. I'm currently knee-deep in AutoIt, so I used that. You can get the tool itself at autoitscript.com.

Notes:

  • I bound Ctrl+Shift+I to Word's InsertFile command, you should do the same for the script to work. Google it or look up on StackExchange.

  • The script works from assumption that an empty document is already open in MS Word and that there is only one MS Word window.

  • Modify the value of $locationOfRtfFiles to match your setup. It must point to the folder containing the RTFs.

  • The sleep interval may require adjustment (depends on performance of your computer and the size/complexity of RTF files)

The code (a bit over 10 lines, but let's hope no one is counting):

; Some common sense stuff, look it up in the docs
AutoItSetOption("TrayIconDebug", 1)
AutoItSetOption("MustDeclareVars", 1)
; Tell AutoIt to match the substring anywhere in the window title
AutoItSetOption("WinTitleMatchMode", 2)

; find a window by title and some contained text (optional),
; make it active and wait for the window to become
; active
Func MyWinWait($title, $keytext = "")
    WinWait($title, $keytext)
    WinActivate($title, $keytext)
    WinWaitActive($title, $keytext)
    Return
EndFunc

Local $locationOfRtfFiles = "C:\MyCollectionOfRtfPorn"

; for every file in the set
For $fileIndex = 1 to 70000
    ; focus on the main Word window
    MyWinWait("Microsoft Word")

    ; emulate Ctrl+Shift+I
    Send("{CTRLDOWN}{SHIFTDOWN}i{SHIFTUP}{CTRLUP}")

    ; wait for the Insert File window to open
    MyWinWait("Insert File")
    ; type out a file's name
    Send($locationOfRtfFiles & "\" & $fileIndex & ".rtf")
    ; confirm selection
    Send("{ENTER}")

    ; sleep for 10 seconds before proceeding to next file
    Sleep(10000)
Next

You may want to adjust the cycle limit based on the actual number of RTFs. Also, if the naming of the files does not follow the numbering scheme you originally specified, the script will need changes.

4
  • 1) Thanks A LOT! Spasibo bol'shoe, rabotaet! Ja tozhe russkij! (/Thanks a lot, it works. I'm also Russian/) 2) It's beautiful, but a bit primitive, and as soon as you go under 1 sec sleep, there is no real effect. That is, => it is slow. Which is the main weakness of this script. But I'm impressed and it's a working solution :) I'm familiar with "Macro Expert", so I wonder how one could speed up this script, to below 1 sec per file. Otherwise I'm looking at days of merging.
    – Andrew
    May 12, 2012 at 21:11
  • Andrew, you are welcome. The issue with shortening the wait time is that AutoIt (in this case) has no reliable way of knowing that the insertion has completed. A VBA script would not have this problem, of course, so I Googled out a ready-made example for you :) blogs.technet.com/b/heyscriptingguy/archive/2005/05/03/… I believe this should work at the maximum speed attainable with InsertFile. A different approach may get better performance, but I sincerely hope you do not need to merge 70k RTFs on a regular basis. :)
    – minya
    May 12, 2012 at 22:11
  • The last piece of code on that site works fine, BUT it doesn't take the files in the order, by which it is ordered in the directory. Instead of 1.rtf, 2.rtf, it takes 1.rtf, then 10321.rtf, etc. Can this be fixed somehow? Thanks :)
    – Andrew
    May 13, 2012 at 0:51
  • stackoverflow.com/questions/5417979/… (use the batch script from that Question; it needs a little bit of adaptation)
    – minya
    May 13, 2012 at 8:24
1

If you are using a unix OS use the following:

textutil -cat rtf *.rtf *.doc -output combinedFiles.rtf
3
  • 1
    I thought .doc, .rtf and pretty much any Microsoft document format had a header and the resultant file would require to have only one header?
    – LawrenceC
    May 12, 2012 at 21:05
  • I'm on Windows 7, as noted above, so unfortunately, that is not a solution.
    – Andrew
    May 12, 2012 at 21:13
  • 1
    The textutil command doesn't exist on most Unix systems. It's on MacOSX, and probably on other BSD-derived systems, but not on Ubuntu, CentOS, Solaris, or Cygwin. May 12, 2012 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .