Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do combine (append) multiple rtf files in the current working directory from within Stata?

    I have multiple rtf files in my current working directory (produced using Ben Jann's -estout- (on SSC)). I would like to combine the rtf files into a single rtf file, appending them one after the other, at the end of the do-file with which I create the rtf files.

    After web search on the DOS -copy- command (I use Stata 16 in a Windows 10 environment), I thought that I would be able to do something like:

    Code:
    shell copy f1.rtf+f2.rtf  combined.rtf
    If I do this, I get no error message, but the 'combined.rtf' file contains only one of the files.

    Any tips about to achieve what I want, please?

  • #2
    Each RTF file has a begging and an ending. The ending is usually marked with the curly bracket "}". Once a document is ended, any content thereafter is out of the context of that document. When you combine these files, they are appended after the ending of the first file, and hence the appended text is ignored.

    In asdoc (from SSC), I programmatically find the ending of a given file, delete it, and append more text to it, therefore, all statistics can be written to the same document as shown in this video
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      It is possible that the rtfutil package written by Roger Newson and available from SSC provides utilities that will do what you need without having to resort to the Windows shell.

      Comment


      • #4
        Thank you, Attaullah and William, for your helpful advice. I've now checked out -rtfutil-, but I don't think it does what I want. (However, I've written to Roger Newson privately to ask some more specific questions.) The information about RTF's markup language is helpful; I've learnt things. Including the fact that DOS -copy- did create the combined file as requested (as I can now see using a text editor); it's just that MS Word did not show all the material -- for the reasons Attaulah states. For my specific application, about which I didn't give all the details, I'm currently thinking that the route of "programmatically find(ing) the ending of a given file, delete it" will be my solution.
        thanks again ...

        Comment


        • #5
          Perhaps Stephen and others have already thought of this, but for a Stata-ish way to remove the "end of text" markers in the case described here, and then save the file, I would point to the potential of -fileread(-), and -filewrite()- used on a strL. I suspect I'm missing some niceties of how end-of-file and/or end-of-text are marked in rtf files (I presume Attaullah can help here), but I'm thinking of something like this:
          Code:
          local eot = "whatever is used for end of text in rtf"
          gen strL s = fileread("combined.rtf") in 1
          replace s = subinstr(s, "`eot'", "", .) in 1
          gen long b = filewrite("combined.rtf", s + "`eot'", 1) in 1

          Comment


          • #6
            This is a fun way to spend a Sunday.

            From Wikipedia I find the following example of an RTF document.

            Code:
            {\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
            This is some {\b bold} text.\par
            }
            I used this to create test1.rtf which mimics what Stephen did.
            Code:
            {\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
            This is some {\b bold} text.\par
            }
            {\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
            This is some {\i italic} text.\par
            }
            and indeed when Word opens it, it indeed stops at the end of the first document, displaying just a single paragraph.

            But Wikipedia is somewhat helpful - in particular, telling us that an RTF document consists of "groups" contained within braces - and with some experimentation, that led me to create test2.rtf
            Code:
            {\rtf1\ansi
            {\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
            This is some {\b bold} text.\par
            }
            {\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
            This is some {\i italic} text.\par
            }
            }
            which wraps the concatenated RTF documents within an enclosing RTF document. Word opens test2.rtf and displays two paragraphs as desired.

            Click image for larger version

Name:	RTF.png
Views:	1
Size:	30.7 KB
ID:	1565441

            <mic drop >

            If this works generally, it may be a robust, simple solution that doesn't involve the risk of creating mangled RTF syntax when you are appending possibly unrelated RTF documents. Or else it doesn't work generally, but has been an interesting diversion.

            Comment


            • #7
              Please do keep having fun! If it's as easy as adding 2 lines to a combined file, I'll be a happy person! Thanks very much for looking into this; I shall report back

              Comment


              • #8
                I write to confirm that William Lisowski's suggestion in #6 to enclose the entire concatenated document between "{\rtf1\ansi" and "}" does work in my context. Many thanks to William (and to Roger Newson ) for their assistance here and off-line.

                Comment


                • #9
                  For those who come across this topic later:

                  To what I wrote above, I will add that this solution seems to be specific to opening the combined RTF document in Microsoft Word. RTF is not a well-defined spec, and other editors with RTF capability, such as WordPad on Windows and TextEdit on macOS, may not render combined RTF documents as expected. I would recommend opening the combined RTF document in Word and then saving it in Word's native .docx format. And then carefully review the Word document to ensure that the RTF was interpreted correctly.

                  In summary, I think this technique will work relatively reliably for combining similar RTF documents - multiple outputs from estout in this case - but may have problems combining disparate documents - for example, two documents with different font tables. For those, more work seems to be needed.

                  Comment

                  Working...
                  X