+ Reply to Thread
Results 1 to 4 of 4

Copy Folder then rename files in new folder from list in Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    05-20-2020
    Location
    USA
    MS-Off Ver
    10
    Posts
    28

    Copy Folder then rename files in new folder from list in Excel

    Hello,

    I am attempting to copy a folder and then rename the files in the copied folder with a list derived from an excel workbook. In the excel workbook, column A contains the original name, and Column B contains the new name. Here is a snapshot of the naming "from" and "to" cells:

    2021-04-19_16-41-33.png

    This code that I have so far just copies the folder. I need to now rename the files as determined by the list given above.

    Function GetFolder() As String
        Dim fldr As FileDialog
        Dim sItem As String
        Dim strPath As String
        Dim filesys
        Set filesys = CreateObject("Scripting.FileSystemObject")
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = "Select a Folder"
            .AllowMultiSelect = False
            .InitialFileName = Application.DefaultFilePath
            If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
        End With
    NextCode:
        GetFolder = sItem
        ' Set fldr = Nothing
        strPath = Left(sItem, InStrRev(sItem, "\") - 1)
        strPath = filesys.BuildPath(strPath, "Copy1")
        ' Create the folder "Copy1"
        filesys.CopyFolder sItem, strPath
    End Function

  2. #2
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Copy Folder then rename files in new folder from list in Excel

    Here is some logic found elsewhere, step thru the code and make sure that you are passing the correct "path"/ "filename" into the line that names

    Sub RenameFile()
        Dim z As String
        Dim s As String
        Dim V As Integer
        Dim TotalRow As Integer
        
        TotalRow = ActiveSheet.UsedRange.Rows.Count
        
        For V = 1 To TotalRow
            
            ' Get value of each row in columns 1 start at row 2
            z = Cells(V + 1, 1).Value
            ' Get value of each row in columns 2 start at row 2
            s = Cells(V + 1, 2).Value
            
            Dim sOldPathName As String
            sOldPathName = z
            On Error Resume Next
            Name sOldPathName As s
            
        Next V
        
        MsgBox "Congratulations! You have successfully renamed all the files"
        
    End Sub

  3. #3
    Registered User
    Join Date
    05-20-2020
    Location
    USA
    MS-Off Ver
    10
    Posts
    28

    Re: Copy Folder then rename files in new folder from list in Excel

    Hello,

    Thanks for your response.

    Where do I insert your code?

    I tried the code below but could not get it to work:

    Function GetFolder() As String
        Dim fldr As FileDialog
        Dim sItem As String
        Dim strPath As String
        Dim filesys
        Set filesys = CreateObject("Scripting.FileSystemObject")
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = "Select a Folder"
            .AllowMultiSelect = False
            .InitialFileName = Application.DefaultFilePath
            If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
        End With
    NextCode:
        GetFolder = sItem
        ' Set fldr = Nothing
        strPath = Left(sItem, InStrRev(sItem, "\") - 1)
        strPath = filesys.BuildPath(strPath, "Copy1")
        ' Create the folder "Copy1"
        filesys.CopyFolder sItem, strPath
    End Function
    
    Sub RenameFile()
        Dim z As String
        Dim s As String
        Dim V As Integer
        Dim TotalRow As Integer
        
        TotalRow = ActiveSheet.UsedRange.Rows.Count
        
        For V = 1 To TotalRow
            
            ' Get value of each row in columns 1 start at row 2
            z = Cells(V + 1, 1).Value
            ' Get value of each row in columns 2 start at row 2
            s = Cells(V + 1, 2).Value
            
            Dim sOldPathName As String
            sOldPathName = z
            On Error Resume Next
            Name sOldPathName As s
            
        Next V
        
        MsgBox "Congratulations! You have successfully renamed all the files"
        
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Copy Folder then rename files in new folder from list in Excel

    Depends when you want to rename the files..
    You can call it separately and let it rename the files based on a folder
    Or you can call the rename right after your copy line, but you would need to update the routine to accept the path and file name for it to work.

    
    filesys.CopyFolder sItem, strPath
    
    Call RenameFile()

+ 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. Copy Files from One Folder to Another Folder based on a List In Excel
    By civram1982 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-21-2019, 06:34 AM
  2. Replies: 18
    Last Post: 02-18-2019, 07:42 AM
  3. Rename files in folder with the list of new names
    By sajnica89bg in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-09-2019, 08:12 AM
  4. [SOLVED] Select Folder and Rename Files according to list
    By mediocrerecord in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-23-2018, 02:17 AM
  5. 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
  6. Copy excel and text files from one folder to another folder...
    By annupojupradeep in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2014, 06:34 AM
  7. Replies: 0
    Last Post: 03-04-2013, 02:37 PM

Tags for this Thread

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