April 4th, 2006

How Can I Rename a Word Document Using the First Three Characters in That Document?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I open a Word document, read the first three characters, and then rename the file using those three characters?

— DG

SpacerHey, Scripting Guy! AnswerScript Center

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 sure you want our help when it comes to renaming files?

No, hey, just kidding: after all, just because we don’t do a very good job of naming our files doesn’t mean that we couldn’t do a good job of naming our files (assuming we tried a little harder). (Editor’s Note: You may have noticed the switch from “he” to “we.” Rest assured, not all the Scripting Guys are as disorganized as the Scripting Guy who writes this column.) For example, here’s 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:

Set objWord = CreateObject(“Word.Application”)

Set objDoc = objWord.Documents.Open(“c:\scripts\test.doc”)

Set objRange = objDoc.Range(0,3) strNewName = “C:\Scripts\” & objRange.Text & “.doc”

objWord.Quit Wscript.Sleep 3000

Set objFSO = CreateObject(“Scripting.FileSystemObject”) objFSO.MoveFile “C:\Scripts\Test.doc”, strNewName

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:

\

/

:

*

?

<

>

|

In this script we don’t bother to check whether any of those characters show up among the first three characters in the document. Ideally you’d want a script that took the first three valid characters in the document as opposed to the first three characters. We won’t get into how you might do that today; if there’s enough interest we’ll explore that in a future column. But it’s something to keep in mind.

Second, this script doesn’t 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’re going to run into problems if the first three characters in two or more of those documents happens to be, say, The. In a more polished script, you’d check for the existence of a file (for example, The.doc) before trying to save it. If that file already exists, then you’d need a contingency plan for giving the new file an alternate name. Again, just something to keep in mind.

For now, however, we won’t worry about any of those things. Instead, we’ll simply point out that the script we did come up with begins by creating an instance of the Word.Application object, then uses the Open method to open the file C:\Scripts\Test.doc.

Note. That’s amazing: how in the world did you know that our sample file would be named Test.doc? We mean besides the fact that all our files seem to be named Test.doc.

With the document open we then use this line of code to create a Range object encompassing the first three characters in the document (the Range starts directly after character 0 and ends directly after character 3):

Set objRange = objDoc.Range(0,3)

And once we know the first three characters in the document we can use this line of code to construct a new file name:

strNewName = “C:\Scripts\” & objRange.Text & “.doc”

As you can see, our new file name is composed of three items:

C:\Scripts\. This is, obviously, the path to the C:\Scripts folder. Note, however, that we need to stick a \ at the end.

objRange.Text. The Text property contains all the text that appears in our Range. In this case, that means the first three characters in the document (for example, The).

.doc. The file extension for our new file (seeing as how we’re dealing with Word documents).

Put them all together and they’ll spell something like this:

C:\Scripts\The.doc

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.

Speaking of renaming a file, here’s how we do that:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
objFSO.MoveFile “C:\Scripts\Test.doc”, strNewName

Yes, it’s a little cryptic; for one thing, you won’t see a Rename method anywhere. All we’re doing, however, is creating an instance of the Scripting.FileSystemObject and then calling the MoveFile method. MoveFile requires two parameters: the path to the existing file and the path we want the file to be “moved” to. In this case we’re starting with a file that has the path C:\Scripts\Test.doc and “moving” 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?

We know that this will be the next question: “Is there any way I can do this to all the Word documents in the C:\Scripts folder?” So here’s a sample script that does just that. We won’t 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 FileSystemObject section of the Microsoft Windows 2000 Scripting Guide.

Here’s the multi-file script:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(“C:\Scripts”)

Set objWord = CreateObject(“Word.Application”)

For Each strFile in objFolder.Files arrNames = Split(strFile.Name, “.”) If arrNames(1) = “doc” Then Set objDoc = objWord.Documents.Open(strFile.Path) Set objRange = objDoc.Range(0,3) strNewName = “C:\Scripts\” & objRange.Text & “.doc” objDoc.Close Wscript.Sleep 3000 objFSO.MoveFile strFile.Path, strNewName End If Next

objWord.Quit

Finally, we should also put your mind to ease by noting that this Scripting Guy did 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 better than some of the names proposed by the Scripting Spouse. Dylan has no idea how lucky he is to be named Dylan.)

Author

0 comments

Discussion are closed.