November 9th, 2004

Can I Use a Script to Rename All the Files in a Folder?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Can I use a script to rename all the files in a directory and include a “pl-” prefix plus the original file name?

— JP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JP. It’s interesting how many people need to do something similar: they need to rename all the files in a given folder, either by appending the date, changing the file extension, or – in this case – tacking pl- to the beginning of each file name. Well, never let it be said that the Script Guys don’t listen to Microsoft’s customers. (We don’t, we just don’t want it to ever be said!) Here’s a script that does exactly what you want:

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

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

For Each objFile In colFileList strNewName = objFile.Drive & objFile.Path & “pl-” & _ objFile.FileName & “.” & objFile.Extension errResult = objFile.Rename(strNewName) Next

If you’re planning on modifying this script to meet your own needs, there are two key points to keep in mind. First, notice that we use an Associators Of query in order to return a collection of all the files found in a folder (in this case, the folder C:\Logs):

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

An Associators Of query does pretty much what the name implies: it enables you to associate two WMI classes. In this case, we’re associating Win32_Directory (the class that lets us manage folders) with CIM_DataFile (the class that lets us manage files). You might think that the Win32_Directory class would have a property named Files that would list all the files found in that folder. For some reason, though, it doesn’t. Instead, we have to use an Associators Of query to achieve the same effect.

As you might expect, a complete discussion of Associators Of would never fit in this little column. For more information, check out this portion of the WMI SDK. For now, just copy the code as-is, and – if necessary – change C:\Logs to the appropriate folder.

The other thing to keep in mind is the fact that when you rename a file using WMI, you must pass the entire file path to the Rename method. Suppose you want to rename the file C:\Logs\File_1.txt to C:\Logs\Pl-File_1.txt. This line of code won’t do it:

errResult = objFile.Rename(“Pl-File_1.txt”)

Instead, you’ll have to use this line of code:

errResult = objFile.Rename(“C:\Logs\Pl-File_1.txt”)

That’s why the code preceding the Rename method looks so complicated: we have to construct the entire path for our new file name, assign it to the variable strNewName, then pass strNewName to the Rename method. Here’s how the code breaks down:

objFile.Drive

Returns the drive letter (in this case C:\) for the file we are renaming.

objFile.Path

Don’t let the name fool you: the Path property returns only the folder paths, excluding the drive and the file name. For example, if you are connected to the file C:\Logs\File1_txt, the Path property returns Logs\. If you were connected to C:\Scripts\Logs\Admin Logs\File_1.txt, the Path would be Scripts\Logs\Admin Logs\.

“pl-“

The prefix we want to tack on to the front of each file name.

objFile.FileName

The current name of the file excluding the file extension(in this case, File_1).

“.”

The period that comes between the file name and the file extension. The Extension property (see below) only returns the characters included in the file extension (e.g,, txt); it does not return the period.

objFile.Extension

The file extension of the file. Suppose we wanted to change all these files from .txt to .log. In that case, we wouldn’t need to use the existing file extension; instead, we’d append .log to the end of the file name.

In other words:

objFile.Drive

C:\

objFile.PathLogs\

“pl-“

pl-

 

objFile.FileName

File_1

“.”

.

objFile.Extension

txt

Add them all together – C:\ + Logs\ + pl- + File_1 + . + txt – and you have the new name for the file.

Author

0 comments

Discussion are closed.