[Excel VBA] How to Rename Files and Folders Using FSO

Bulk renaming, such as “adding today’s date to multiple report filenames in a folder,” is a frequent task in file organization.

By using the FileSystemObject (FSO), you can easily rename files and folders in VBA. In this article, I will explain the basic method of renaming using the .Name property of File and Folder objects, along with important points to note.

目次

Preparation: Reference Settings

To use FSO comfortably, I recommend enabling the reference. In the VBA Editor, go to Tools > References and check “Microsoft Scripting Runtime”.

1. Batch Rename All Files in a Folder

First, here is practical code to add the current date to all filenames in a specified folder.

VBA Code

' Reference: Microsoft Scripting Runtime
Sub RenameAllFilesInFolder()

    ' Declare variables
    Dim fso As New FileSystemObject
    Dim targetFolder As Folder
    Dim fileObj As File
    Dim folderPath As String
    
    '--- Path of the folder containing files to rename ---
    folderPath = ThisWorkbook.Path & "\Reports\"

    ' If folder does not exist, stop processing
    If Not fso.FolderExists(folderPath) Then
        MsgBox "Target folder not found.", vbCritical
        Exit Sub
    End If
    
    '--- 1. Get the target folder object ---
    Set targetFolder = fso.GetFolder(folderPath)

    '--- 2. Loop through all files in the folder ---
    For Each fileObj In targetFolder.Files
        '--- 3. Build new filename and assign to .Name property ---
        ' Using GetBaseName (name without extension) and GetExtensionName (extension only)
        fileObj.Name = fso.GetBaseName(fileObj.Path) & "_" & Format(Date, "yyyymmdd") & "." & fso.GetExtensionName(fileObj.Path)
    Next fileObj
    
    MsgBox "Renaming of all files in the folder is complete."
    
    ' Release objects
    Set fileObj = Nothing
    Set targetFolder = Nothing
    Set fso = Nothing

End Sub

2. Rename a Folder

You can rename folders themselves in the same way as files.

VBA Code

Sub RenameSingleFolder()
    Dim fso As New FileSystemObject
    Dim folderObj As Folder
    Dim originalFolderPath As String
    
    originalFolderPath = ThisWorkbook.Path & "\OldFolderName"
    
    If fso.FolderExists(originalFolderPath) Then
        Set folderObj = fso.GetFolder(originalFolderPath)
        
        ' Just assign the new name to the .Name property
        folderObj.Name = "NewFolderName"
        
        MsgBox "Folder name changed."
    Else
        MsgBox "Target folder not found."
    End If
End Sub

Explanation of Key Points

Assigning to the .Name Property

The core of renaming with FSO is very simple.

TargetObject.Name = "New Name"

The .Name property of File and Folder objects is not just for reading; it is also writable. Just by assigning a new name string to this property, the actual file or folder name changes.

Constructing the New Filename

fileObj.Name = fso.GetBaseName(fileObj.Path) & "_" & ...

When adding dates to filenames, you need to separate the extension from the original filename. The FSO methods .GetBaseName (gets filename without extension) and .GetExtensionName (gets extension only) ensure this process is done correctly.

[Important] Points to Note When Renaming

Renaming files or folders causes a runtime error under the following conditions:

  1. File is Open: If the file you are trying to rename is open in Excel or another application, a “Permission denied” error occurs.
  2. Name Duplication: If the new name already exists in the same folder, an error occurs. It is safer to check with fso.FileExists beforehand.
  3. Invalid Characters: Including characters that cannot be used in filenames (\ / : * ? " < > |) in the new name will result in an error.

Summary

Renaming using FSO is very easy once you understand the .Name property.

  1. Get the target object using fso.GetFile or fso.GetFolder.
  2. Assign the new name string to its .Name property.

The process of bulk renaming files in a folder is very useful for file organization. Please pay attention to the error conditions and try using it!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次