Renaming a File

Microsoft® Windows® 2000 Scripting Guide

The FileSystemObject does not include a direct method for renaming a file. However, in much the same way that a folder can be renamed using the MoveFolder method, files can be renamed using the MoveFile method. To rename a file, call the MoveFile method but leave the file in its current folder.

For example, the script in Listing 4.26 renames ScriptLog.txt to BackupLog.txt. Technically, the script actually moves C:\FSO\ScriptLog.txt to a new path: C:\FSO\BackupLog.txt. The net result, however, is that the file named ScriptLog.txt is now named BackupLog.txt.

Listing 4.26 Renaming a File Using the MoveFile Method

  
1
2
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"