{"id":70113,"date":"2005-04-01T16:58:00","date_gmt":"2005-04-01T16:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/01\/how-can-i-convert-1000-rtf-files-to-word-documents\/"},"modified":"2005-04-01T16:58:00","modified_gmt":"2005-04-01T16:58:00","slug":"how-can-i-convert-1000-rtf-files-to-word-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-convert-1000-rtf-files-to-word-documents\/","title":{"rendered":"How Can I Convert 1,000 .RTF Files to Word Documents?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>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?<BR><BR>&#8212; LS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, LS. Sure, we can help you. We\u2019re not going to, but we could if we wanted to.<\/P>\n<P>No, wait, come back: we\u2019re just <I>kidding<\/I>. Not only can we help you, but we <I>will<\/I> help you. To prove it, let\u2019s 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:<\/P><PRE class=\"codeSample\">Const wdFormatDocument = 0<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nSet objDoc = objWord.Documents.Open(&#8220;c:\\scripts\\test.rtf&#8221;)\nobjDoc.SaveAs &#8220;C:\\Scripts\\test.doc&#8221;, wdFormatDocument<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>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 <B>Word.Application<\/B> object, then use the <B>Open<\/B> method to open the document C:\\Scripts\\Test.rtf.<\/P>\n<P>So how do we convert this document to Word format? Well, technically, we don\u2019t: we aren\u2019t going to actually <I>convert<\/I> the document, we\u2019re going to save a copy of it in Word format. When we\u2019re done we\u2019ll have two files: Test.doc and Test.rtf. If you don\u2019t really <I>want<\/I> two files, you could add a couple lines of code and have the script delete Test.rtf.<\/P>\n<P>Here\u2019s the line of code that creates Test.doc. As you can see, we call the <B>SaveAs<\/B> 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):<\/P><PRE class=\"codeSample\">objDoc.SaveAs &#8220;C:\\Scripts\\test.doc&#8221;, wdFormatDocument\n<\/PRE>\n<P>We then call Word\u2019s <B>Quit<\/B> method (to dismiss Word) and we\u2019re done. Just that easy, just that quick.<\/P>\n<P>Of course, while <I>we<\/I> might be done, you\u2019re 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:<\/P><PRE class=\"codeSample\">Const wdFormatDocument  = 0<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    If objFile.Extension = &#8220;rtf&#8221; Then\n        strFile = &#8220;C:\\Scripts\\&#8221; &amp; objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n        strNewFile = &#8220;C:\\Scripts\\&#8221; &amp; objFile.FileName &amp; &#8220;.doc&#8221;\n        Set objDoc = objWord.Documents.Open(strFile)\n        objDoc.SaveAs strNewFile, wdFormatDocument\n        objDoc.Close\n    End If\nNext<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>We won\u2019t 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\u2019s what this crazy-looking query does:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>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 <B>Extension <\/B>property is equal to RTF (note that\u2019s 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:<\/P><PRE class=\"codeSample\">strFile = &#8220;C:\\Scripts\\&#8221; &amp; objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n<\/PRE>\n<P>We then construct a name for our Word version of this file using this line of code:<\/P><PRE class=\"codeSample\">strNewFile = &#8220;C:\\Scripts\\&#8221; &amp; objFile.FileName &amp; &#8220;.doc&#8221;\n<\/PRE>\n<P>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 <I>this<\/I> 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:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nSet objDoc = objWord.Documents.Open(strFile)\nobjDoc.SaveAs strNewFile, wdFormatDocument\nobjDoc.Close\n<\/PRE>\n<P>Note that, after calling the SaveAs method, we use the <B>Close<\/B> method to get rid of Test.rtf but leave Word running. We then loop around and look for the next RTF file. After we\u2019ve finished going through the entire collection, we exit the loop and <I>then<\/I> 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.<\/P>\n<P>Speaking of which \u2026. 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.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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?&#8212; LS Hey, LS. Sure, we can help you. We\u2019re not going to, but we could if we wanted to. No, wait, come back: we\u2019re just [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5],"class_list":["post-70113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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?&#8212; LS Hey, LS. Sure, we can help you. We\u2019re not going to, but we could if we wanted to. No, wait, come back: we\u2019re just [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70113\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}