How Can I Convert 1,000 .RTF Files to Word Documents?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a folder of about 1,000 .RTF files. I need to convert all of these to Word documents (.DOC files). Can you help me with this?

— LS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, LS. Sure, we can help you. We’re not going to, but we could if we wanted to.

No, wait, come back: we’re just kidding. Not only can we help you, but we will help you. To prove it, let’s start by showing you a script that can open a single .RTF file (C:\Scripts\Test.rtf) and then create a Word version of this document:

Const wdFormatDocument = 0

Set objWord = CreateObject(“Word.Application”) Set objDoc = objWord.Documents.Open(“c:\scripts\test.rtf”) objDoc.SaveAs “C:\Scripts\test.doc”, wdFormatDocument


See how easy that is? We begin by creating a constant named wdFormatDocument and assigning it the value 0; this happens to be the value that represents a standard Word document. We create an instance of the Word.Application object, then use the Open method to open the document C:\Scripts\Test.rtf.

So how do we convert this document to Word format? Well, technically, we don’t: we aren’t going to actually convert the document, we’re going to save a copy of it in Word format. When we’re done we’ll have two files: Test.doc and Test.rtf. If you don’t really want two files, you could add a couple lines of code and have the script delete Test.rtf.

Here’s the line of code that creates Test.doc. As you can see, we call the SaveAs method, passing as parameters the new file path (C:\Scripts\Test.doc) and the constant wdFormatDocument (which, again, tells Word to save the file as a Word document):

objDoc.SaveAs “C:\Scripts\test.doc”, wdFormatDocument

We then call Word’s Quit method (to dismiss Word) and we’re done. Just that easy, just that quick.

Of course, while we might be done, you’re not: after all, you have an entire folder full of files to convert. Can you get a script to convert all those files for you? You bet:

Const wdFormatDocument  = 0

strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFiles = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Scripts’} Where ” _ & “ResultClass = CIM_DataFile”)

Set objWord = CreateObject(“Word.Application”)

For Each objFile in colFiles If objFile.Extension = “rtf” Then strFile = “C:\Scripts\” & objFile.FileName & “.” & objFile.Extension strNewFile = “C:\Scripts\” & objFile.FileName & “.doc” Set objDoc = objWord.Documents.Open(strFile) objDoc.SaveAs strNewFile, wdFormatDocument objDoc.Close End If Next


We won’t spend a lot of time detailing this script, but we will at least give you an overview of what it does. We begin by connecting to the WMI service and then retrieving a list of all the files found in the C:\Scripts folder. That’s what this crazy-looking query does:

Set colFiles = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Scripts’} Where ” _
        & “ResultClass = CIM_DataFile”)

We create an instance of the Word.Application object, and then set up a For Each loop and walk through the collection, checking each file to see if the Extension property is equal to RTF (note that’s just the letters RTF, without a period). Each time we find a .RTF file, we use this line of code to construct the file path and store it in the variable strFileName:

strFile = “C:\Scripts\” & objFile.FileName & “.” & objFile.Extension

We then construct a name for our Word version of this file using this line of code:

strNewFile = “C:\Scripts\” & objFile.FileName & “.doc”

Got that? If we found a file named Test.rtf, the variable strFileName now holds this value: C:\Scripts\Test.rtf. Likewise, the variable strNewFile will now hold this value: C:\Scripts\Test.doc. We can then use the code we showed you at the beginning of this column, the code that opens a .RTF file and saves it as a .DOC file. The only difference? Instead of hard-coding in file names we use our variables strFile and strNewFile:

Set objWord = CreateObject(“Word.Application”)
Set objDoc = objWord.Documents.Open(strFile)
objDoc.SaveAs strNewFile, wdFormatDocument

Note that, after calling the SaveAs method, we use the Close method to get rid of Test.rtf but leave Word running. We then loop around and look for the next RTF file. After we’ve finished going through the entire collection, we exit the loop and then use the Quit method to terminate our instance of Word. We do it this way to keep from having to repeatedly start up Word, terminate it, and then restart when we find another .RTF file. That process would actually work, but would definitely slow the script down.

Speaking of which …. Based on both the number of files and the size of those files, this script can take a little while to run. In our tests, it took about 45 seconds to open and save three different .RTF files, each about 4 megabytes in size. So, plan accordingly.


Comments are closed. Login to edit/delete your existing comments