Rename all Files in a Folder
2 min readJan 20, 2022
Get File Names from Folder:
Sub LoopThroughFiles()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
FolderName = Range("D2").Value
Set oFolder = oFSO.GetFolder(FolderName)
i = 1For Each oFile In oFolder.Files
Cells(i + 1, 1) = oFile.Name
i = i + 1
Next oFile
End Sub
Rename all File Names:
Sub RenameAllFilenamesInAFolder()
Dim intRowCount As Integer
Dim intCtr As Integer
Dim strFileNameExisting As String
Dim strFileNameNew As String
Dim strFolder As String
'Set the folder path
strFolder = Range("D2").Value
With Sheets("Sheet_Rename")
'Find the total rows count in the sheet
'This will be the last non-blank cell in column A...
intRowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop through from the 2nd row (1st row is Heading)
'till the total rows in the sheet
For intCtr = 2 To intRowCount
'Get the existing filename from the cell
strFileNameExisting = .Range("A" & intCtr)
'Get the new filename from the cell
strFileNameNew = .Range("B" & intCtr)
'Rename the file
Name strFolder & strFileNameExisting As strFolder & strFileNameNew
Next intCtr
End With
'Display an appropriate message, once complete
MsgBox "All files renamed successfully!", _
vbInformation, "All files renamed"
End Sub
GitHub:
https://github.com/havishmad/excel_rename_files_in_folder
YouTube:
https://youtu.be/BiM70ObANCM