Macro create folder based on created date for all of files in directory

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello

I have many files in directory C:\sales , contains many files . so what I want arranging the files by create multiple folders based on created date for all of files . for instance if i have some files created in month JAN then should create folder's name is SALES-JAN-2022 and move the files created date in month JAN to created new folder SALES-JAN-2022 and if the files created in month FEB then should create folder's name is SALES-FEB-2022 and move the files created date in month FEB to created new folder SALES-FEB-2022 . this should happen for each year .

thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hopefully this resolves the problem : You can just change 'MyFolPath' and everything else as same.

VBA Code:
Sub MoveMonthlyFilesToRespectiveFolders()
Sheets.Add.Name = "FileNames"
Sheets("FileNames").Activate
MyFolPath = "C:\sales"
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(MyFolPath)
For Each oFile In oFolder.Files
    Cells(i + 1, 1) = oFile.Name
    Cells(i + 1, 2) = FileDateTime(oFolder & "\" & oFile.Name)
    Cells(i + 1, 3) = MonthName(Month(Cells(i + 1, 2))) & "_" & Year(Cells(i + 1, 2))
    i = i + 1
Next oFile
Range("C1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("D1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$D$1:$D$1048576").RemoveDuplicates Columns:=1, Header:=xlNo
i = 1
Do While Cells(i, 4) <> "" 'Loop through to create folders
    MkDir MyFolPath & "\" & Cells(i, 4).Value
    i = i + 1
Loop
i = 1
Dim FSO As Object
Dim SourceFileName As String, DestinFileName As String
Set FSO = CreateObject("Scripting.Filesystemobject")
Do While Cells(i, 1) <> "" 'Loop through to move files
    SourceFileName = MyFolPath & "\" & Sheets("FileNames").Cells(i, 1).Value
    DestinFileName = MyFolPath & "\" & Cells(i, 3).Value & "\" & Sheets("FileNames").Cells(i, 1).Value
    FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName
    i = i + 1
Loop
End Sub

Let me know if you are finding any issues.
 
Upvote 0
gives error application defined error in this line
VBA Code:
Sheets.Add.Name = "FileNames"
 
Upvote 0
Make sure there are no sheets named "FileNames" in your excel file.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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