{"id":67613,"date":"2006-04-04T14:16:00","date_gmt":"2006-04-04T14:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/04\/how-can-i-rename-a-word-document-using-the-first-three-characters-in-that-document\/"},"modified":"2006-04-04T14:16:00","modified_gmt":"2006-04-04T14:16:00","slug":"how-can-i-rename-a-word-document-using-the-first-three-characters-in-that-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-rename-a-word-document-using-the-first-three-characters-in-that-document\/","title":{"rendered":"How Can I Rename a Word Document Using the First Three Characters in That Document?"},"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! How can I open a Word document, read the first three characters, and then rename the file using those three characters?<BR><BR>&#8212; DG<\/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, DG. To tell you the truth, we always get a chuckle any time we get a question that asks for our help in renaming files or folders. Why? Well, the Scripting Guy who writes this column has essentially one folder on his computer, a desktop folder named Stuff. Inside that folder he has a few subfolders, including folders named Junk, New Folder, and New Folder (2). And inside those folders he has files with names like X.vbs, Test.txt, Sample.doc, and Hold.xls. Are you <I>sure<\/I> you want our help when it comes to renaming files?<\/P>\n<P>No, hey, just kidding: after all, just because we <I>don\u2019t<\/I> do a very good job of naming our files doesn\u2019t mean that we <I>couldn\u2019t<\/I> do a good job of naming our files (assuming we tried a little harder).<I> (Editor\u2019s Note: You may have noticed the switch from \u201che\u201d to \u201cwe.\u201d Rest assured, not all the Scripting Guys are as disorganized as the Scripting Guy who writes this column.)<\/I> For example, here\u2019s a script that should help you with your problem: the script opens a Word document, reads the first three characters, and then renames the file using those characters:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;c:\\scripts\\test.doc&#8221;)<\/p>\n<p>Set objRange = objDoc.Range(0,3)\nstrNewName = &#8220;C:\\Scripts\\&#8221; &amp; objRange.Text &amp; &#8220;.doc&#8221;<\/p>\n<p>objWord.Quit\nWscript.Sleep 3000<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjFSO.MoveFile &#8220;C:\\Scripts\\Test.doc&#8221;, strNewName\n<\/PRE>\n<P>Before we explain how the script works we should issue a couple of cautions. As you probably know, there are certain characters that cannot be used in a file or folder name; these prohibited characters include:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\\<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\/<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>:<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>*<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>?<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201c<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>&lt;<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>&gt;<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>|<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In this script we don\u2019t bother to check whether any of those characters show up among the first three characters in the document. Ideally you\u2019d want a script that took the first three <I>valid<\/I> characters in the document as opposed to the first three characters. We won\u2019t get into how you might do that today; if there\u2019s enough interest we\u2019ll explore that in a future column. But it\u2019s something to keep in mind.<\/P>\n<P>Second, this script doesn\u2019t verify that a file name is unique before attempting to save the file. That could be a problem if you tried running this script against multiple Word documents in the same folder; after all, you\u2019re going to run into problems if the first three characters in two or more of those documents happens to be, say, <B>The<\/B>. In a more polished script, you\u2019d check for the existence of a file (for example, The.doc) before trying to save it. If that file already exists, then you\u2019d need a contingency plan for giving the new file an alternate name. Again, just something to keep in mind.<\/P>\n<P>For now, however, we won\u2019t worry about any of those things. Instead, we\u2019ll simply point out that the script we <I>did<\/I> come up with begins by creating an instance of the <B>Word.Application<\/B> object, then uses the <B>Open<\/B> method to open the file C:\\Scripts\\Test.doc.<\/P>\n<TABLE id=\"ERE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. That\u2019s amazing: how in the world did you know that our sample file would be named Test.doc? We mean besides the fact that <I>all<\/I> our files seem to be named Test.doc.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>With the document open we then use this line of code to create a <B>Range<\/B> object encompassing the first three characters in the document (the Range starts directly after character 0 and ends directly after character 3):<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Range(0,3)\n<\/PRE>\n<P>And once we know the first three characters in the document we can use this line of code to construct a new file name:<\/P><PRE class=\"codeSample\">strNewName = &#8220;C:\\Scripts\\&#8221; &amp; objRange.Text &amp; &#8220;.doc&#8221;\n<\/PRE>\n<P>As you can see, our new file name is composed of three items:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>C:\\Scripts\\<\/B>. This is, obviously, the path to the C:\\Scripts folder. Note, however, that we need to stick a \\ at the end.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>objRange.Text<\/B>. The <B>Text<\/B> property contains all the text that appears in our Range. In this case, that means the first three characters in the document (for example, <I>The<\/I>).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>.doc<\/B>. The file extension for our new file (seeing as how we\u2019re dealing with Word documents).<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Put them all together and they\u2019ll spell something like this:<\/P><PRE class=\"codeSample\">C:\\Scripts\\The.doc\n<\/PRE>\n<P>After we construct the new file name we exit Word and then pause the script for three seconds (3000 milliseconds). We insert the pause simply to give Word a chance to close and, in the process, relinquish its grip on the file. Without a pause of some kind the script might try to rename the file while Word is still running and thus still has the file open. As you might expect, your script will fail if it tries to rename a file that another application is using.<\/P>\n<P>Speaking of renaming a file, here\u2019s how we do that:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjFSO.MoveFile &#8220;C:\\Scripts\\Test.doc&#8221;, strNewName\n<\/PRE>\n<P>Yes, it\u2019s a little cryptic; for one thing, you won\u2019t see a Rename method anywhere. All we\u2019re doing, however, is creating an instance of the <B>Scripting.FileSystemObject<\/B> and then calling the <B>MoveFile<\/B> method. MoveFile requires two parameters: the path to the existing file and the path we want the file to be \u201cmoved\u201d to. In this case we\u2019re starting with a file that has the path C:\\Scripts\\Test.doc and \u201cmoving\u201d it to a file that has the path C:\\Scripts\\The.doc. It might seem like a silly way to do things, but the net result is that the file gets renamed. Which is all we really care about, right?<\/P>\n<P>We know that this will be the next question: \u201cIs there any way I can do this to all the Word documents in the C:\\Scripts folder?\u201d So here\u2019s a sample script that does just that. We won\u2019t walk you through this code today, but you should find most of it self-explanatory. If you still have questions about how it works, take a look at the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_scr_xfxi.mspx\" target=\"_blank\"><B>FileSystemObject<\/B><\/A> section of the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P>\n<P>Here\u2019s the multi-file script:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.GetFolder(&#8220;C:\\Scripts&#8221;)<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)<\/p>\n<p>For Each strFile in objFolder.Files\n    arrNames = Split(strFile.Name, &#8220;.&#8221;)\n    If arrNames(1) = &#8220;doc&#8221; Then\n        Set objDoc = objWord.Documents.Open(strFile.Path)\n        Set objRange = objDoc.Range(0,3)\n        strNewName = &#8220;C:\\Scripts\\&#8221; &amp; objRange.Text &amp; &#8220;.doc&#8221;\n        objDoc.Close\n        Wscript.Sleep 3000\n        objFSO.MoveFile strFile.Path, strNewName\n    End If\nNext<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>Finally, we should also put your mind to ease by noting that this Scripting Guy <I>did<\/I> have some help when it came to naming his son. Which explains why he has a son named Dylan and not a son named Junk, Test, Stuff, or Sample. (Although, to be honest, all of those are actually <I>better<\/I> than some of the names proposed by the Scripting Spouse. Dylan has no idea how lucky he is to be named Dylan.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I open a Word document, read the first three characters, and then rename the file using those three characters?&#8212; DG Hey, DG. To tell you the truth, we always get a chuckle any time we get a question that asks for our help in renaming files or folders. Why? Well, [&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-67613","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! How can I open a Word document, read the first three characters, and then rename the file using those three characters?&#8212; DG Hey, DG. To tell you the truth, we always get a chuckle any time we get a question that asks for our help in renaming files or folders. Why? Well, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67613","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=67613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67613\/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=67613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}