【VBA】Change file name

You can change file name!

PR

VBA Code

As an example, change the file name of the file “old.txt” to “new.txt”.

File before change
File before change
Option Explicit

Sub sampleProc()
    
    Dim fso As Object
    Dim fileFullPath As String
    Dim newFileName As String
    
    'Set "File path before change"
    fileFullPath = "C:\Users\user\Desktop\old.txt"
    'Set "File name after change"
    newFileName = "new.txt"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Rename file
    fso.GetFile(fileFullPath).Name = newFileName
    
    Set fso = Nothing
    
End Sub

Set “File path before change”(Line 10).

Set “File name after change”(Line 12).

Rename file(Line 17).

PR

Result

You changed file name!

Result
Result
PR

FYI

For more information about following, see the following official online manual:

GetFile method” of “FileSystemObject”


Name property” of “File Object”

タイトルとURLをコピーしました