Forum Discussion

PatMyer's avatar
PatMyer
Copper Contributor
Feb 24, 2023

How to rename file names from an excel cell

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 I pulled from to excel.  I have tried creating a batch file from Notepad and saving/executing from the folder but that did not work. Can this be done?

 

 

 

  • PatMyer 

    Oh wait - my bad. The values in the worksheet already include .png.

    Try this version:

    Sub RenameFiles()
        ' Change the path as needed, but keep the trailing backslash
        Const sFolder = "C:\MyFiles\"
        Dim r As Long
        Dim m As Long
        Dim v As Variant
        m = Range("A" & Rows.Count).End(xlUp).Row
        v = Range("A1:B" & m).Value
        On Error GoTo ErrHandler
        For r = 2 To m
            Name sFolder & v(r, 1) As sFolder & v(r, 2)
        Next r
        Exit Sub
    ErrHandler:
        MsgBox "Failed to rename " & v(r, 1), vbInformation
        Resume Next
    End Sub

     

  • PatMyer 

    Could you create a list of the original names in column A, and the corresponding modified names in column B?

    • PatMyer's avatar
      PatMyer
      Copper Contributor

      HansVogelaar 

      Ok, same file names are now in Column A that are currently in the folder and what I want the .png file names changed to are in Column B (added _ after the 591_42).

      By the way, this is just a sample size to test, the folder of images I want to update has over 10,000 .png files.

       

       

       

Share

Resources