Renaming a Folder

Microsoft® Windows® 2000 Scripting Guide

The FileSystemObject does not include a method, such as RenameFolder, that provides an obvious way to rename a folder. However, you can rename a folder by using the MoveFolder method and maintaining the same relative location. For example, suppose you have a folder with the following path:

C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Current Logs

If you rename the folder by using the Rename command in Windows Explorer, the path remains identical except for the endpoint, the folder itself:

C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Archived Logs

The MoveFolder method enables you to achieve the same end result by moving the folder from C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Current Logs to C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Archived Logs. The net result is exactly the same as that of using Windows Explorer to rename the folder.

For example, the script in Listing 4.11 uses MoveFolder to rename the folder C:\FSO\Samples to C:\FSO\Scripts. Before the script runs, Samples is the only subfolder in C:\FSO. After the script runs, Scripts is the only subfolder in C:\FSO. Furthermore, Scripts contains all the files and subfolders previously contained in Samples.

Listing 4.11 Renaming a Folder Using the MoveFolder Method


Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\FSO\Samples" , "C:\FSO\Scripts"