+ Reply to Thread
Results 1 to 11 of 11

Rename multiple files in a folder with a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    08-29-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    83

    Rename multiple files in a folder with a macro

    Hi,

    I have multiple files in a folder and I want to rename them automatically via a macro. I use Excel 2016.

    BEFORE

    The existing file names are:

    Firstname Surname.html
    OR
    Firstname Surname SOMETEXT.html
    OR
    Firstname Surname SOMETEXT_SOMETEXT.html

    AFTER

    I would like the macro to rename the files to:

    Firstname Surname _ Live.html
    or
    Firstname Surname SOMETEXT_SOMETEXT_Live.html

    Can you help me with this request?

    Thank you,
    Ash
    Last edited by nobleprince; 08-27-2016 at 04:41 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Rename multiple files in a folder with a macro

    Add this code to a standard codemodule in the .xlsm file, and set a reference to MS Scripting Runtime. Save the workbook in the folder with all the html files to be renamed prior to running the macro. Of course, change SOMETEXT to the actual text that you have - if it isn't the same for all files, delete the first Name line and uncomment the second.

    Sub RenameFiles()
        Dim objShell As Object
        Dim objFolder As Object
        Dim objFile As Object
    
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace(ThisWorkbook.path)
    
        For Each objFile In objFolder.Items
            If objFile.Name Like "*.html" Then
     
               Name objFile.Name As Replace( Replace( objFile.Name, "SOMETEXT", ""), ".html", "_Live.html")
               'Name objFile.Name As Replace( objFile.Name,  ".html", "_Live.html")
    
            End If
        Next objFile
        
    End Sub
    Last edited by Bernie Deitrick; 08-24-2016 at 09:29 AM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    08-29-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Rename multiple files in a folder with a macro

    Hi Bernie,

    Thanks for this. I tried both options and none of them work.

    The file names are different for each file. The requirement is only to add "_Live" to the end of file names.

    Any advise?

    Ash

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Rename multiple files in a folder with a macro

    Try the following .. Put your files in a folder named "Test Folder" in the same path of thisworkbook
    Sub Rename_Files()
        Dim fls     As Variant
        Dim f       As Variant
    
        Set fls = GetFiles(ThisWorkbook.Path & "\Test Folder\", "*.html")
        
        For Each f In fls
            Name f As Replace(f, ".html", "_Live.html")
        Next f
        
        MsgBox "Done...", 64
    End Sub
    
    Function GetFiles(strPath As String, Optional strPattern As String = "") As Collection
        Dim rv      As New Collection
        Dim f       As Variant
    
        If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
        f = Dir(strPath & strPattern)
    
        Do While Len(f) > 0
            rv.Add strPath & f
            f = Dir()
        Loop
    
        Set GetFiles = rv
    End Function
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Rename multiple files in a folder with a macro

    Did you save the workbook in the same folder? I used Book2.xlsm:

    Capture.JPG

    Capture after.JPG

    Sub RenameFiles()
        Dim objShell As Object
        Dim objFolder As Object
        Dim objFile As Object
    
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace(ThisWorkbook.Path)
    
        For Each objFile In objFolder.Items
            If objFile.Name Like "*.html" Then
     
               Name objFile.Name As Replace(objFile.Name, ".html", "_Live.html")
    
            End If
        Next objFile
        
    End Sub

  6. #6
    Registered User
    Join Date
    08-29-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Rename multiple files in a folder with a macro

    The "Test Folder" macro worked OK but only when I put the Macro outside of the folder.

    The last (smaller) macro did not work even if I put it inside or outside the folder.
    Please note there are lots of folders with different names and within these folders there are lots of html files.
    The folder names are in the following format:

    Profiles saved as HTML file for yyy.com
    Profiles saved as HTML file for zzz.com
    etc

    The file names are:
    Firstname Surname.html
    Firstname Middlename Surname.html
    etc

    Any suggestions? Thanks again.

  7. #7
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Rename multiple files in a folder with a macro

    This code requires a reference to MS Scripting Runtime - place the Excel Workbook in the root folder, and all the HTML files will be renamed:

    Option Explicit
    Dim FSO As Scripting.FileSystemObject
    Sub RenameAllHTMLFilesInFolderTree()
        Dim strFolderName As String
        Dim fsoFolder As Scripting.Folder
        
        If FSO Is Nothing Then
            Set FSO = New Scripting.FileSystemObject
        End If
            
        strFolderName = ThisWorkbook.Path     'Change to your actual folder
        Set fsoFolder = FSO.GetFolder(strFolderName)
        FindAndRenameFiles fsoFolder
        
    End Sub
    
    Sub FindAndRenameFiles(fsoPFolder As Scripting.Folder)
        Dim fsoFile As Scripting.File
        Dim fsoSFolder As Scripting.Folder
            
        For Each fsoFile In fsoPFolder.Files
            If LCase(fsoFile.Name) Like "*.html" Then
                Name fsoPFolder.Path & "\" & fsoFile.Name As Replace(fsoPFolder.Path & "\" & fsoFile.Name, ".html", "_Live.html")
            End If
        Next fsoFile
        
        For Each fsoSFolder In fsoPFolder.SubFolders
            FindAndRenameFiles fsoSFolder
        Next fsoSFolder
        
    End Sub

  8. #8
    Registered User
    Join Date
    08-29-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Rename multiple files in a folder with a macro

    Thanks Bernie. All working good. Just the MS Scripting Runtime required updating.

    Any chance you could have a look at the following:
    The requirement is to move multiple html files in multiple folders to a single (another) folder.
    http://www.excelforum.com/excel-prog...ne-folder.html

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Rename multiple files in a folder with a macro

    Ok - take a look over there, too.

  10. #10
    Registered User
    Join Date
    08-29-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Rename multiple files in a folder with a macro

    I am getting an error in the macro.

    The error happens when there are special chars in file names:
    Matt Taylor ☁
    Александр БойкO
    Андрей ОконникоB
    Виталий ЩербаньA

    Plus there could be other special chars, etc.

    Please advise?
    Last edited by nobleprince; 08-24-2016 at 06:51 PM.

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Rename multiple files in a folder with a macro

    My system doesn't allow special characters, so I cannot test it. You should start a new thread to ask about that.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Rename .pdf files in a folder
    By vijanand1279 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-11-2019, 01:11 AM
  2. Rename Multiple PDF files in a folder per the list in Excel spreadsheet
    By vmanil75 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2017, 09:40 AM
  3. Macro to move files from one folder to another but rename duplicates
    By mark_anthony in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2014, 06:56 AM
  4. Macro to open consecutively .TIFF files in 1 folder and rename them?
    By tedikoleva in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-09-2014, 03:28 PM
  5. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  6. Replies: 0
    Last Post: 03-04-2013, 02:37 PM
  7. [SOLVED] Rename multiple files in folder
    By MissaLissa in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-17-2013, 04:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1