+ Reply to Thread
Results 1 to 8 of 8

VBA to rename files and move them to a different folder

Hybrid View

  1. #1
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Question VBA to rename files and move them to a different folder

    On my computer I have a source folder:
    C:\Users\jonat\Documents\Finance

    And also a folder for a weekly archiving process.
    C:\Users\jonat\Documents\Archive

    In the source folder I have my weekly files. I am using 5 for this example, but in reality I have far more.
    Taunton - Current Week.xlsx
    Newbury - Current Week.xlsx
    Exeter - Current Week.xlsx
    Plymouth - Current Week.xlsx
    Bristol - Current week.xlsx

    I am hoping someone can provide some vba code which will:-
    Rename all the files in the source folder, replacing Current Week with the Period in cell B1 and the Week in cell B2.
    And move the renamed files from the source folder to the archive folder.

    So for example.
    Before vba is run.
    Taunton - Current Week.xlsx starts in source folder.
    After vba is run
    We have Taunton - P13 W1.xlsx in archive folder.

    I change the variables in cell B1 and B2 each week.
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    189

    Re: VBA to rename files and move them to a different folder

    Try it

    Sub demo()
    Const source_folder = "C:\Users\jonat\Documents\Finanse"
    Const archive_folder = "C:\Users\jonat\Documents\Archive"
    Dim destfile As String, B1B2 As String, fso As Object, folder As Object, f As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If fso.FolderExists(source_folder) And fso.FolderExists(archive_folder) Then
            With Worksheets("Sheet1")
                B1B2 = .Range("B1").Value & " " & .Range("B2").Value
            End With
            Set folder = fso.GetFolder(source_folder)
            For Each f In folder.files
                destfile = Replace(f.Name, "Current Week", B1B2, , , vbTextCompare)
                fso.MoveFile f.Path, archive_folder & "\" & destfile
            Next f
        End If
        Set fso = Nothing
    End Sub

  3. #3
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: VBA to rename files and move them to a different folder

    I've closed the post as solved, but had one question if you don't mind.
    I have one or two admin files in my source folder, so could you provide an extra code on the f.Name part, so it only does the rename and move if the file contains "Current Week" somewhere in the name.

    Many thanks for this extra question.

  4. #4
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    189

    Re: VBA to rename files and move them to a different folder

    Quote Originally Posted by picton2000 View Post
    I've closed the post as solved, but had one question if you don't mind.
    I have one or two admin files in my source folder, so could you provide an extra code on the f.Name part, so it only does the rename and move if the file contains "Current Week" somewhere in the name.

    Many thanks for this extra question.
    I added red
    Sub demo()
    Const source_folder = "C:\Users\jonat\Documents\Finanse"
    Const archive_folder = "C:\Users\jonat\Documents\Archive"
    Dim destfile As String, B1B2 As String, fso As Object, folder As Object, f As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If fso.FolderExists(source_folder) And fso.FolderExists(archive_folder) Then
            With Worksheets("Sheet1")
                B1B2 = .Range("B1").Value & " " & .Range("B2").Value
            End With
            Set folder = fso.GetFolder(source_folder)
            For Each f In folder.files
                destfile = Replace(f.Name, "Current Week", B1B2, , , vbTextCompare)
                If destfile <> f.Name Then fso.MoveFile f.Path, archive_folder & "\" & destfile
            Next f
        End If
        Set fso = Nothing
    End Sub

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,941

    Re: VBA to rename files and move them to a different folder

    Another way

    Sub Test()
    Dim p$, s$, d$
    
    With Sheet1
        p = .[B1] & " " & .[B2]
        s = .[A7] & "\"
        d = .[A10] & "\"
    End With
    
    If Len(Dir(s)) And Len(Dir(d)) Then
        f = Dir(s & "*.xlsx")
        Do While f <> ""
            Name s & f As d & Replace(f, "Current Week", p)
            f = Dir
        Loop
    End If
    
    End Sub

  6. #6
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: VBA to rename files and move them to a different folder

    Thank you to the two replies, excellent work.

  7. #7
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    189

    Re: VBA to rename files and move them to a different folder

    Quote Originally Posted by picton2000 View Post
    Thank you to the two replies, excellent work.
    For clarity I mention that you are English and for you it makes no difference. But someone not English can have an alphabet that contains unicode, and he has a path that contains unicode, he can't use the code from DIR. Because DIR doesn't support unicode. My code is written for a wider range of users.

  8. #8
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,941

    Re: VBA to rename files and move them to a different folder

    Just add the highlighted text


    Sub Test()
    Dim p$, s$, d$
    
    With Sheet1
        p = .[B1] & " " & .[B2]
        s = .[A7] & "\"
        d = .[A10] & "\"
    End With
    
    If Len(Dir(s)) And Len(Dir(d)) Then
        f = Dir(s & "*.xlsx")
        Do While f <> ""
           If InStr(f, "Current Week") Then Name s & f As d & Replace(f, "Current Week", p)
            f = Dir
        Loop
    End If
    
    End Sub

+ 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. [SOLVED] Loop through all files in a folder and rename the files considering a pattern
    By excelactuary in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-22-2022, 10:57 AM
  2. [SOLVED] Copy Folder then rename files in new folder from list in Excel
    By jimjones1958 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2021, 05:00 PM
  3. Ability to rename multiple files from a path and move and rename to another.
    By X82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2020, 09:54 PM
  4. Replies: 1
    Last Post: 01-28-2019, 06:55 PM
  5. Replies: 18
    Last Post: 11-27-2018, 05:25 AM
  6. 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
  7. [SOLVED] Move, Rename all files from a folder
    By kinseld5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-26-2012, 12:20 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