sequentially rename multiple files within specific subfolders

CaptainKen

New Member
Joined
Oct 31, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I found this post that seems similar to what I need to do, but not quit what I need.

I have a list of numerous folders in Excel such as the example below, where I need to rename files using prefix "fanart" with suffix of sequential numbers within each respective "extrafanart" folder. Within each fanart folder the numbering would start from zero. So file names: fanart1.jpg, fanart2.jpg, etc.

Video\Movies\A Million Ways to Die in the West (2014)\extrafanart
Video\Movies 4K\Black Panther - Wakanda Forever (2022)\extrafanart
Video\Movies\Movie Sets\American Pie Collection\American Pie 1 (1999)\extrafanart
Video\Movies\Movie Sets\American Pie Collection\American Pie 2 (2001)\extrafanart
Video\Movies\Movie Sets\American Pie Collection\American Pie 3 American Wedding (2003)\extrafanart
Video\Movies\Movie Sets\American Pie Collection\American Pie Presents Band Camp (2005)\extrafanart
Video\Movies\Movie Sets\American Pie Collection\American Pie Presents Beta House (2007)\extrafanart

As you can see in the above examples the files to be sequentially renamed will always be in a folder called "extrafanart".
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this macro, after changing the folder path string to the location of your "\Video\" folder and it will rename all the files in all the subfolders of "\Video\" which end with "\extrafanart".

To test it on just one folder, specify the folder string "C:\path\to\Video\Movies\A Million Ways to Die in the West (2014)\", for example.

VBA Code:
Public Sub Rename_Files_In_Folders()       
    Rename_Files_In_Folder "C:\path\to\Video\"            'CHANGE THIS FOLDER PATH  
    MsgBox "Done" 
End Sub


Private Sub Rename_Files_In_Folder(folderPath As String)

    Static FSO As Object
    Dim thisFolder As Object, subfolder As Object
    Dim thisFile As Object
    Dim p As Long, n As Long
    
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set thisFolder = FSO.GetFolder(folderPath)
        
    If InStrRev(thisFolder.Path, "\extrafanart", -1, vbTextCompare) = Len(thisFolder.Path) - Len("\extrafanart") + 1 Then
        'This folder path ends with "\extrafanart", so rename the files in it
        n = 0
        For Each thisFile In thisFolder.Files
            p = InStrRev(thisFile.Name, ".")
            If p > 0 Then
                n = n + 1
                thisFile.Move thisFile.ParentFolder & "\fanart" & n & Mid(thisFile.Name, p)
            End If
        Next
    End If
    
    'Do subfolders
    
    For Each subfolder In thisFolder.SubFolders
        Rename_Files_In_Folder subfolder.Path
    Next
    
End Sub
 
Upvote 0
Solution
Thanks you John!

I copied and pasted this into a vbs file and changed the path, but I'm getting the error shown here.

Snag_8788ef0.png


It's pointing to the "A" in As within this line
Private Sub Rename_Files_In_Folder(folderPath As String)
 
Upvote 0
The code is VBA, not VBScript, and should be copied and pasted into a module in an Excel workbook, saved as a macro-enabled workbook (.xlsm file).
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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