Batch append file names based on fields in Excel

AnOriginal

New Member
Joined
Feb 14, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello.

Is there a way batch append file names based on fields within an Excel spreadsheet?

I have 770 files which all have different filenames. I would like to rename the files with fields associated with each file name by appending it with values from mulitple columns.

For example, Column H2 has a generic value SPYB-8691.png. It needs to be appended with identifying values from the following columns A2_B2_C2_D2_E2_SPYB-8691.png (team_jersey#_firstname_lastname_originalfilename.png).

Each file name is unique but the A, B, C, D and E columns should have identical values.

Is this possible to do?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
My understanding is that the existing file names are in column H, starting at H2 and they should be renamed using values in columns A:E (prepend to file name).

Try this macro, after editing the folder path containing the files, where indicated.
VBA Code:
Public Sub Rename_Files()

    Dim folderPath As String
    Dim fields As Variant
    Dim i As Long
    
    folderPath = "C:\Path\to\folder\"    '<----- CHANGE THIS PATH
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    
    With ActiveSheet
        fields = .Range("A1", .Cells(.Rows.Count, "H").End(xlUp)).Value
    End With
    
    For i = 2 To UBound(fields)
        If Dir(folderPath & fields(i, 8)) <> vbNullString Then
            Name folderPath & fields(i, 8) As folderPath & fields(i, 1) & "_" & fields(i, 2) & "_" & fields(i, 3) & "_" & fields(i, 4) & "_" & fields(i, 5) & "_" & fields(i, 8)
        Else
            MsgBox "Cell H" & i & " " & folderPath & fields(i, 8) & " file not found"
        End If
    Next
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Solution
OMG! Simply amazing! I appreciate you getting back to me so quickly. It all worked perfectly. Thank you so much! You're a genius!
 
Upvote 0
OMG! Simply amazing! I appreciate you getting back to me so quickly. It all worked perfectly. Thank you so much! You're a genius!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,069
Members
449,090
Latest member
fragment

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