Rename multiple parts of the same file name for all files in a directory

sxschech

Registered User.
Local time
Today, 07:03
Joined
Mar 2, 2010
Messages
802
Sharing this idea...

Had a project to rename a bunch of scanned documents. Was given a name convention to use. After spending some time manually renaming by keying in the same information over and over decided to see about automating the repeating value portion of the rename. I had a rename utility, but because needed to rename multiple parts of the same document and there were a few conditions, ended up writing some code to loop through the directory and rename that way. Using this method can rename as many times within the same filename as needed and if the pattern is set up properly, will handle conditions without having to use if or case statements. It uses the replace function in a loop. Pass in the phrase or phrases to find and then pass in the replacement values. Since in the situation all the scanned files had to be renamed from a generic filename such as SKM20220306100422.pdf, I was able to define the initial file name in order to take advantage of the code to produce the final result. Otherwise need to be careful because if the item to replace is contained in other parts of the file name, it would get replaced there, leading to unintended renames. The first step was to rename the files with an abbreviation that I could search for. So gave that part of the file name -- and then replaced it with - INV, the second rename was -NF to - NOT FOUND and a third replacement was -xx with - 25906.

Note: In the code, when passing in the values, it is set up for all the items to find will be in one group followed by the group to replace, rather than find/replace, find/replace. Code contains an example in the comments.

Code:
Sub RenameMultiFiles(SourceFolder As String, FindWhat As String, ReplaceWith As String, Optional sepchar As String = ",")
'Rename all files in a folder that meet the criteria.  This sub can rename a file that will have different parts renamed
'Can leave off trailing slash and can choose sep char, if don't put in a sep char, the default is comma
'
'Example: Replace -- with - INV also replace -NF. with - NOT FOUND. and replace -xx with - 25906
'
'Call RenameMultiFiles("C:\Temp","--,-NF.,-xx", " - INV,- NOT FOUND.,- 25906")
'
'---OLD NAME---                         ---NEW NAME---
'General Supply Inc--551122 -NF.pdf     ==> General Supply Inc - INV 551122 - NOT FOUND.pdf
'General Supply Inc--551123 -xx-845.pdf ==> General Supply Inc - INV 551123 - 25906-845.pdf
'
'Modified from
'https://debugvba.com/rename-multiple-files-in-a-folder-or-directory-by-vba-excel-debugvba-com/
'20211228
    Dim objFileSystem As Object
    Dim OriginalFile As Variant
    Dim RenamedFile As String
    Dim FileLocation As String
    Dim FindItem As Variant
    Dim ReplaceItem As Variant
    Dim I As Integer
    Dim renCount As Integer
    
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    'add final \ slash to Path of the folder where files are located if missing
    If Right(SourceFolder, 1) <> "\" Then SourceFolder = SourceFolder & "\"
    'Check if source folder exists
    If objFileSystem.FolderExists(SourceFolder) = True Then
        'Looping through each file in the source folder
        For Each OriginalFile In objFileSystem.GetFolder(SourceFolder).Files
            'Loop through list of findwhat and replacewith
            FindItem = Split(FindWhat, sepchar)
            ReplaceItem = Split(ReplaceWith, sepchar)
            'Loop through list of FindItems
            For I = 0 To UBound(FindItem)
                'Check if selected file meets criteria
                If InStr(OriginalFile, FindItem(I)) Then
                    FileLocation = objFileSystem.GetParentFolderName(OriginalFile)
                    'Since there are multiple items being replaced, only refer to OriginalFile the first time
                    If RenamedFile = "" Then
                        RenamedFile = FileLocation & "\" & Replace(Dir(OriginalFile), FindItem(I), ReplaceItem(I))
                    Else
                        RenamedFile = Replace(RenamedFile, FindItem(I), ReplaceItem(I))
                    End If
                End If
                'Rename original file if it has been renamed only after all replacements have been made
                If I = UBound(FindItem) And RenamedFile <> "" Then
                    Name OriginalFile As RenamedFile
                    RenamedFile = ""
                    renCount = renCount + 1
                End If
            Next
        Next OriginalFile
    Else
        MsgBox "Source folder does not exist"
    End If
        MsgBox "Renamed " & renCount & " file(s)", vbInformation + vbOKOnly, "Rename Complete"
End Sub
 
Hi. Thanks for sharing!
 

Users who are viewing this thread

Back
Top Bottom