create folders based on extensions files & moved files

Ali M

Active Member
Joined
Oct 10, 2021
Messages
348
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
hi experts

I want macro create folder's name is FILES-2022 based on current year in this directory "C:\Users\PC-ALM\Desktop\files" which contains variety files of differnt extensions .
so folder FILES-2022 should create many folders based on files extensions are existed in this directory "C:\Users\PC-ALM\Desktop\files" for instance FILE_PDF, FILE_DOCX ......

and should create the months folders inside folders extensions (FILE_PDF, FILE_DOCX ......)like JAN,FEB and moves the files extensions from "C:\Users\PC-ALM\Desktop\files"to are relating with folder extensions into folder month based on the month .
thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
paste into a module, run: MakeNewYrFolders

Code:
sub MakeNewYrFolders()
dim vDir, vBase, vMoDir, vYr
dim i as byte

vYr = year(date())
vBase  = "C:\Users\PC-ALM\Desktop\files\"
vDir =vBase &  "FILES-" & vYr
MakeDir vDir
for i = 1 to 12
   vMoDir = format(i & "/1/" & vYr,"MMM")
   makedir   vDir & "\" &  vMoDir
next
msgbox "Done"
end sub


Public Sub MakeDir(ByVal pvDir)
Dim fso
On Error GoTo errMake
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(pvDir) Then fso.CreateFolder pvDir     'MkDir pvDir
Set fso = Nothing
Exit Sub
errMake:
'MsgBox Err.Description & vbCrLf & pvDir, , "MakeDir(): " & Err
Set fso = Nothing
End Sub
 
Upvote 0
thanks for your trying

your macro doesn't move the files from directory to the months and doesn't create folders extensions .

my requirement after create FILES-2022 should create folders extensions like FILE_ PDF,FILE-DOCX then create folders month JAN ,FEB for each folder extensions and move the files are relating folders extensions .

hope this details help you
 
Upvote 0
Try this macro. If run today (April 2022), it moves all files from "C:\Users\PC-ALM\Desktop\files" to "C:\Users\PC-ALM\Desktop\files\FILES-2022\FILE_<file extension>\APR\", creating the subfolders as needed.

VBA Code:
Public Sub Move_Files_To_Year_Extension_Month_Subfolder()
    
    Dim sourceFolder As String, destMainFolder As String, destSubfolder As String
    Dim Wsh As Object
    Dim FSO As Object
    Dim FSsourceFolder As Object
    Dim FSfile As Object
    
    sourceFolder = "C:\Users\PC-ALM\Desktop\files\"
    destMainFolder = "C:\Users\PC-ALM\Desktop\files\"
    
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destMainFolder, 1) <> "\" Then destMainFolder = destMainFolder & "\"
    
    Set Wsh = CreateObject("WScript.Shell")
    Set FSO = CreateObject("Scripting.FileSystemObject")
  
    Set FSsourceFolder = FSO.GetFolder(sourceFolder)
    For Each FSfile In FSsourceFolder.Files
        destSubfolder = destMainFolder & "FILES-" & Year(Date) & "\FILE_" & UCase(Mid(FSfile.Name, InStrRev(FSfile.Name, ".") + 1)) & "\" & UCase(Format(Date, "MMM")) & "\"
        If Not FSO.FolderExists(destSubfolder) Then
            Wsh.Run "cmd /c MKDIR " & Chr(34) & destSubfolder & Chr(34), 0, True
        End If
        Debug.Print "MOVE " & FSfile.Path, destSubfolder
        FSfile.Move destSubfolder
    Next

End Sub
 
Upvote 0
Solution
@John_w

I tested your code , it's awesome ! but there is problem it just moves the files based on current month but I have files in JAN ,FEB,MAR .

the code ignores them because depends on current month , how can make it moves all of the files for all of the months?
 
Upvote 0
If I understand you correctly you want to move all the files to subfolders based on the current year and the month of each file's creation date. The only change needed is to the destSubfolder = line:

VBA Code:
        destSubfolder = destMainFolder & "FILES-" & Year(Date) & "\FILE_" & UCase(Mid(FSfile.Name, InStrRev(FSfile.Name, ".") + 1)) & "\" & UCase(Format(FSfile.DateCreated, "MMM")) & "\"
 
Upvote 0
@John_w

thanks again !

it shows another problem. it supposes creating folders of month based on created date of files and moves the files which are relating to month folders .

but the code still depends on current month when moves the files. the code moves files to APRIL folder whatever created date for the files .
it should create folder of month based on created date of files and moves the files to relating folder of month
 
Upvote 0
but the code still depends on current month when moves the files. the code moves files to APRIL folder whatever created date for the files .
Are you sure? Nothing in the revised code looks at the current month; the UCase(Format(FSfile.DateCreated, "MMM")) part converts the month of the file's creation date to JAN, FEB, etc.
 
Upvote 0
Are you sure? Nothing in the revised code looks at the current month; the UCase(Format(FSfile.DateCreated, "MMM")) part converts the month of the file's creation date to JAN, FEB, etc.
this is completely strange !!!!
actually I try with file
created date of file is Tuesday, ‎February ‎1, ‎2022, ‏‎10:06:26 PM
modified date of file is ‎Tuesday, ‎February ‎1, ‎2022, ‏‎10:58:32 PM
the right way should create folder is FEB and move the file to into it , but it just moves to folder APRIL and when see the preporties to see the created date this is what I got
created date of file is Wednesday, ‎April ‎27, ‎2022, ‏‎7:28:22 PM
modified date of fileTuesday, ‎February ‎1, ‎2022, ‏‎10:58:32 PM

why become in APRIL as in created date after moves the file ???
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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