How to rename image file names in a folder from an excel worksheet

PatMyer

New Member
Joined
Jan 30, 2023
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
I have a list of .png file names exported to excel that I have modified (added an _ in a specific location in the name) and want to rename the same .png file names in the same folder to the updated name. I have tried creating a batch file from Notepad and saving/executing from the folder but that did not work. Is there a Marco or Command Prompt code that can be used?
 

Attachments

  • Rename_Image_Files.jpg
    Rename_Image_Files.jpg
    255.4 KB · Views: 38

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi PayMyer,

Here is my suggestion for how to go about this. I would probably do this in 2 separate subroutines. In the first sub, gather the names of all .png files from a particular folder and list them in a column. My code uses column A. Then, you can list your new names in another column (my code uses column B), and run the 2nd subroutine which will rename files with the name listed in column B.
VBA Code:
Sub GetFiles()

Dim fso As Object
Dim folder As Object
Dim file As Object
Dim rowNum As Long

rowNum = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Users\Elena\Desktop\png rename") 'Rename this with the folder that contains your .png files

For Each file In folder.Files
    If Right(file.Name, 4) = ".png" Then
        Cells(rowNum + 1, 1).Value = file.Name
        rowNum = rowNum + 1
    End If
Next

Set fso = Nothing
Set folder = Nothing
Set file = Nothing

End Sub

And here is the 2nd subroutine that will handle the renaming:

VBA Code:
Sub RenameFiles()
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim oldName As String
    Dim newName As String
    Dim filePath As String
    Dim i As Long

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("C:\Users\Elena\Desktop\png rename") ' Replace with your folder path

    For Each file In folder.Files
        oldName = file.Name
        For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
            If oldName = Cells(i, 1).Value Then
                newName = Cells(i, 2).Value
                filePath = folder.Path & "\" & newName
                fso.MoveFile file.Path, filePath
                Exit For
            End If
        Next i
    Next file

    Set fso = Nothing
    Set folder = Nothing
    Set file = Nothing
End Sub

Let us know how it goes for you.

Have a nice weekend,
...Mike
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,813
Members
449,469
Latest member
Kingwi11y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top