Rename all Files in a Folder

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 = 1
For 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

--

--

--

Founder @ Havish M Consulting | 40u40 [Analytics Insight] | AuthorX20 | MOS | MCDA | MCT | Taught at IIM ABCLK

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Networking on AWS provisioned with Terraform

Build your Ruby Gem

An Introduction to Web server

Don’t let that consistent launch time slip away!

Understanding OpenAI baseline source code and making it do self-play! Part 1

Finally! Sorted Lottery Order

Keep it Simple

Git config

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HAVISH MADHVAPATY

HAVISH MADHVAPATY

Founder @ Havish M Consulting | 40u40 [Analytics Insight] | AuthorX20 | MOS | MCDA | MCT | Taught at IIM ABCLK

More from Medium

Basics: class, Set(), and Map()

How To Use HubSpot CRM | Best FREE CRM Software for Small Business (HubSpot Tutorial)

The Power of Networking

OWASP Top 10 — TryHackMe