Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I present users with a dialog box that enables them to rename a file? Ideally I’d like this to be in a loop of some kind, so that they could rename multiple files without having to restart the script.

-- ER

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ER. Before we begin today we’d like to note that on this date, way back in 1854, American entrepreneur C. W. Post was born. Post was the founder of the Postum cereal company, a company which was eventually renamed Post Cereals. Today, Post makes such breakfast staples as Alpha-Bits, Raisin Bran, and Post Toasties, along with both Cocoa Pebbles and Fruity Pebbles!

And no, now that you mention it, we have no idea why C. W. Post’s birthday is not a national holiday. After all, despite his many achievements, even Dr. Martin Luther King, Jr. never invented a cereal like Marshmallow Alpha-Bits.

Note. OK, so, technically, neither did C. W. Post; instead, he’s credited with inventing Grape Nuts and Post Toasties (which, by the way, was originally named Elijah’s Mannah). Interestingly enough, however, the first product developed and sold by the C. W. Post company wasn’t even a breakfast cereal; instead, it was a powdered coffee substitute named Postum, which is made by roasting ingredients such as wheat bran, wheat, molasses, and corn dextrin and then grinding the resulting mass into a powder. Just add hot water and enjoy!

In case you’re wondering, Postum is still sold today. And no, we have no idea why.

Anyway, happy birthday, C. W. Post. The Scripting Guys wanted to do something to mark the occasion, but we weren’t sure what. And then it hit us: what better tribute to the man who brought us Grape Nuts (which was also originally marketed as another coffee substitute; as it turned out, though, people liked eating the stuff way more than they liked drinking it) than to write a script that presents users with a dialog box that enables them to rename files?

OK, so maybe it isn’t all that great of a tribute. But it was the best we could do on such short notice; after all, we didn’t even know it was C. W. Post’s birthday until we heard it on the radio while driving in to work.

Here’s the script:

Set objOpenDialog = CreateObject("UserAccounts.CommonDialog")
Set objSaveDialog = CreateObject("SAFRCFileDlg.FileSave")

objOpenDialog.Filter = "All Files|*.*"
objOpenDialog.InitialDir = "C:\"

Do While True
    objOpenDialog.FileName = ""
    intResult = objOpenDialog.ShowOpen
 
    If intResult = 0 Then
        Wscript.Quit
    Else
        strOldFileName = objOpenDialog.FileName
    End If

    objSaveDialog.FileName = strOldFileName

    intReturn = objSaveDialog.OpenFileSaveDlg

    If intReturn <> 0 Then
        strNewFileName = objSaveDialog.FileName
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        objFSO.MoveFile strOldFileName, strNewFileName
    Else
        Wscript.Quit
    End If
Loop

Very important note. For better or worse (and no doubt a lot of you will say “for worse”), this script works only on Windows XP; it doesn’t work on Windows 2000, Windows Server 2003, Windows Vista, or any other version of Windows. Why not? Because the two dialog box objects needed to carry out this task are available only on Windows XP. (Why were these objects not included in Windows Vista? We have no idea.) Anyway, if you have Windows XP then you’re in business; if you don’t have Windows XP, well, sorry, but we don’t really have a very good answer for you. (Unless you consider calling dialogs boxes from Microsoft Word a good answer. If you do, well, then we do have a good answer for you.)

For those of you who are running Windows XP, we kick things off by creating two different objects: UserAccounts.CommonDialog and SAFRCFileDlg.FileSave. The first object is going to present us with a File Open dialog box; the user will use that dialog box to select the file to be renamed. The second object is going to present us with a File Save dialog box; this is the dialog box the user will use to type in a new name for the previously-selected file.

Incidentally, you can also select a new folder for the file; in that case the file will be moved to the new folder and then named, well, whatever name you typed in the dialog box. That means we have a dual purpose script here: not only can it be used to rename files, but it can also be used to move files. Much like the Post cereal Shredded Wheat and Bran, which combines everyone’s two favorite things – shredded wheat and bran – in a single cereal.

Note. At the risk of interrupting, could you use this script to move multiple files at one time? No, not this script. However, we know people are interested in doing that, so we’ll see if we can tackle that task sometime in the future.

And yes we know: according to the old credo, the future is now. But not here in the Script Center. In the Script Center, even now is sometime in the future.

After we create our objects we then assign values to two of the File Open dialog box’s properties:

objOpenDialog.Filter = "All Files|*.*"
objOpenDialog.InitialDir = "C:\"

The Filter property enables us to show only files of a specified type (e.g., .txt files) in the dialog box. Of course, we don’t want to filter out any file type; therefore, we use the wild card value *.* to indicate that we want to look at all files. Suppose, however, that we were interested only in .txt files. In that case, we would set the filter to the following:

objOpenDialog.Filter = "Text Files|*.txt"

This variation lets you switch between .txt and .csv files:

objOpenDialog.Filter = "Text Files|*.txt|CSV Files|*.csv"

And this variation – well, we digress.

Which is highly unusual for this column.

Meanwhile, the InitialDir property simply instructs the dialog box to, by default, show the C:\ directory each time it starts up. At the risk of digressing even further, here’s a nifty little block of code; this one causes the dialog box to open up in the working directory of the script itself:

Set objShell = CreateObject("Wscript.Shell")
strFolder = objShell.CurrentDirectory
objOpenDialog.InitialDir = strFolder

OK, enough about the InitialDir property. After we assign the property values, we next set up a Do loop designed to run forever (or at least until True is no longer equal to True):

Do While True

Note. Don’t worry; we’ll provide a way for you to escape this seemingly-endless loop. Trust us: when you’re a Scripting Guy you always have to have an escape plan handy.

Inside this loop, we set the File Open dialog box’s FileName property to an empty string; that ensures that a file won’t be pre-selected for you when the dialog box opens. Once that’s done we then go ahead and open the dialog box, something we do by calling the ShowOpen method:

intResult = objOpenDialog.ShowOpen

Like all good dialog boxes, our File Open dialog box is going to remain open – and the script is going to remain “blocked,” until one of two things happens: either we select a file and click Open, or we click the Cancel button. As soon as one of these buttons is clicked the script unblocks and we execute this block of code:

If intResult = 0 Then
    Wscript.Quit
Else
    strOldFileName = objOpenDialog.FileName
End If

All we’re doing here is examining the value of the variable intResult, a variable that gets populated when a user dismisses the File Open dialog box. If intresult is equal to 0 that means the user clicked the Cancel button; in that case, we call the Quit method and terminate the script. (Which, needless to say, also breaks us out of our not-so-endless loop.) If intResult is not equal to 0 (in which case it would be equal to -1) we then use this line of code to assign the path of the selected file (the dialog box’s FileName property) to a variable named strOldFileName:

strOldFileName = objOpenDialog.FileName

Now it’s time to bring up our second dialog box, the File Save dialog box:

intReturn = objSaveDialog.OpenFileSaveDlg

Note. OK, technically, you don’t have to use a second dialog box; instead, you could bring up the first dialog box, locate the file to be renamed, right-click the file (in the dialog box), and then click Rename. That works fine, but it might not be the most intuitive way to go about renaming files. Therefore, we went with the two-dialog box approach.

When the File Save dialog box appears onscreen, we, again, have two options: 1) enter a new name for the file and then click OK, or 2) click Cancel. Once we’ve done either one of those things we trigger this block of code:

If intReturn <> 0 Then
    strNewFileName = objSaveDialog.FileName
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.MoveFile strOldFileName, strNewFileName
Else
    Wscript.Quit
End If

Here we’re using an If Then statement to examine the return value from the dialog box. If intReturn is equal to 0 that means the user clicked Cancel; in that case, we call the Quit method and terminate the script. Otherwise we:

1.

Get the path of the selected file (the value of the FileName property) and store it in a variable named strNewFileName.

2.

Create an instance of the Scripting.FileSystemObject.

3.

Call the MoveFile method to “move” the file from its old path to its new path. In some cases, that might involve actually moving the file from one folder to another. In other cases, the file stays in the same folder but simply gets renamed: for example, C:\Scripts\Test.txt is “moved” to the path C:\Scripts\NewName.txt. In the latter scenario, we’re only “moving” the file in a technical sense; in a practical sense, we’re renaming the file.

And then we loop around, display a new instance of the File Open dialog box, and repeat the process all over again.

We hope that helps, ER. And now that we’ve officially wished C. W. Post a happy birthday we can let everyone in on a secret: we’re actually a little disappointed that it’s C. W.’s birthday rather than John Harvey Kellogg’s birthday. John Harvey Kellogg, whose brother founded the Kellogg Company, was way more interesting than C. W. Post. For example, in addition to helping invent Kellogg’s Corn Flakes, John Harvey was a noted physician (he believed people should eat only twice a day, and only bland food) and social commentator. He and his wife, Ella, were married for 40 years and never once slept together in the same room.

Needless to say, they had no children.