Renaming Folders

Microsoft® Windows® 2000 Scripting Guide

Standardization helps facilitate system administration. For example, having a standard set of folder names makes it easier to run scripts that verify that the proper set of files has been installed on a computer. With standard folder names, scripts such as these can simply connect to the desired folders without needing a list of unique folder names and locations for each individual computer.

Scripts can help you implement standardized naming schemes on computers throughout your organization. For example, the Win32_Directory class provides a Rename method that allows you to rename a folder. Thus, you can write a script that connects to all the domain controllers in your organization and renames the folder C:\Stuff to C:\Administrative Scripts.

To rename a folder, first bind to the folder in question and then call the Rename method. As the sole parameter to the method, pass the new name for the folder as a complete path name. For example, if the folder in the C:\Scripts\Logs\Backup is to be renamed C:\Scripts\Archive, you must pass C:\Scripts\Archive as the complete folder name. Passing only the folder name - Archive - results in an Invalid path error.

Scripting Steps

Listing 11.9 contains a script that binds to the folder C:\Scripts and renames the folder C:\Script Repository. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Directory class.

    To limit data retrieval to a specified folder, a Where clause is included restricting the returned folders to those with the name C:\\Scripts. You must include both backslashes (\\) in the name.

  4. For the single folder in the returned collection, use the Rename method to rename the folder to C:\Script Repository.

  5. Echo the results of the renaming procedure. A 0 indicates that the folder was successfully renamed.

Listing 11.9 Renaming Folders

  
1
2
3
4
5
6
7
8
9
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE Name = 'c:\\Scripts'")
For Each objFolder in colFolders
 errResults = objFolder.Rename("C:\Script Repository")
 Wscript.Echo errResults
Next