Move Excel File based on their file names

Rahulwork

Active Member
Joined
Jun 9, 2013
Messages
284
Hello Everyone,

I am working on one big project and need your small help in the same.

I want to move excel files to folder based on their file names. below is the example

- I have a folder path in A1
- B1 have date : 20-07-2020
- C1 have date : 15-07-2020

in path i have four files:

1. Sample 2020-07-20.xlsx
2. Example 2020-07-20.xlsx
3. Sample 2020-07-15.xlsx
4. Example 2020-07-15.xlsx

i want when i click on button, macro should go to
- A1 path
- then create two folders by the name of B1 and C1
- the move all 2020-07-2020 files to B1 folder and 2020-07-2020 files to C1

please help
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this

Assumptions
- A1 contains valid folder path like ... C:\Folder\Subfolder
- B1 and C1 contain valid dates

Run from sheet containing the required values
VBA Code:
Sub MoveFiles()
    Dim fPath As String, cel As Range, f As String, newFldr As String
    Const d As String = "yyyy-mm-dd"
    fPath = [A1]
    If Len(Dir(fPath, vbDirectory)) > 0 Then
        For Each cel In Union([B1], [C1])
            f = Format(cel.Value, d)
            newFldr = fPath & "\" & f
            If Len(Dir(newFldr, vbDirectory)) = 0 Then MkDir newFldr
            Call LoopThroughFilesInFolder(fPath, "*xlsx", f, fPath, newFldr)
        Next cel
    Else
        MsgBox [A1] & vbCr & "does not exist", , ""
    End If
End Sub

Private Sub LoopThroughFilesInFolder(strDir As String, strType As String, aDate As String, oldF As String, newF As String)
    Dim file As Variant
    strDir = strDir & "\"
    file = Dir(strDir & strType)
    While (file <> "")
        If InStr(file, aDate & ".xlsx") > 0 Then Name oldF & "\" & file As newF & "\" & file
        file = Dir
    Wend
End Sub
 
Upvote 0
Thank you so much for you response. this is helping me to move all files of 20th July in one folder and rest in 17th. it's done

but what if i want to move file basis on their name? like sample file hould move to smple folder and example in example
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,607
Members
449,090
Latest member
vivek chauhan

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