create new folder based on restriction how many files in another folder

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 different extensions files. what I want creating folder name's SALES_2022 and create multiple folders into it and rename it to become SALE1 and put the files in this folder based on 30 files ,when reach 30 files should create new folder SALE2 and put the files in this folder based on 30 files and so on for each folder , when finish the year should creat new year SALES_2023 and create multiple folders into it ,with considering when put the 30 files into folder should be from old date to new date .
I hop to covered all of details
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How do you want to select which 2022 files go into which SALE* folder or does it not matter?
How many files have you got in the SALES_2022 folder?

At the end of 2022 can you manually create a SALES_2023 folder, and sub-folders as and when required, and save the files into the appropriate folder when they are created
or have I got this requirement wrong.
 
Upvote 0
How do you want to select which 2022 files go into which SALE* folder or does it not matter?
what do you mean select?
as I said creating folders automatically
How many files have you got in the SALES_2022 folder?
currently 100 different extensions files and will increase .
At the end of 2022 can you manually create a SALES_2023 folder, and sub-folders as and when required, and save the files into the appropriate folder when they are created
or have I got this requirement wrong.
I search for macro does it automatically without interfer from me
 
Upvote 0
when put the 30 files into folder should be from old date to new date .

I don't understand what you mean.

Try this macro. It first creates the C:\Sales\SALES_2022 subfolder (2022 being the current year) if it doesn't exist, then it moves all the files from C:\Sales, moving the first 30 files to C:\Sales\SALES_2022\SALE1, the next 30 files to C:\Sales\SALES_2022\SALE2, etc., creating new SALE<n> subfolders as needed. If you put more files in C:\Sales and run the macro again it moves the files to the highest numbered SALE<n> subfolder, to the maximum of 30 files and creates new SALE<n> subfolders as needed.
VBA Code:
Public Sub Move_Files_To_Subfolders()
   
    Dim sourceMainFolder As String, destMainFolder As String
    Dim FSO As Object
    Dim FSsourceFolder As Object, FSdestFolder As Object
    Dim FSfile As Object
    Dim yearSubfolder As String
    Dim saleN As Long
   
    Const MAX_FILES_PER_SUBFOLDER = 30
   
    sourceMainFolder = "C:\Sales\"
    destMainFolder = "C:\Sales\"
   
    If Right(sourceMainFolder, 1) <> "\" Then sourceMainFolder = sourceMainFolder & "\"
    If Right(destMainFolder, 1) <> "\" Then destMainFolder = destMainFolder & "\"
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    Set FSsourceFolder = FSO.GetFolder(sourceMainFolder)
   
    'Create the SALES_yyyy subfolder, where yyyy is the current year, in the main destination folder, if it doesn't exist
   
    yearSubfolder = destMainFolder & "SALES_" & Year(Date) & "\"
    If Not FSO.FolderExists(yearSubfolder) Then FSO.CreateFolder yearSubfolder
   
    'Set FSdestFolder to highest numbered SALE<n> (SALE1, SALE2 etc.) subfolder in SALES_yyyy folder, or nothing if there are no SALE<n> subfolders
   
    Set FSdestFolder = Nothing
    saleN = 0
    While FSO.FolderExists(yearSubfolder & "SALE" & saleN + 1)
        saleN = saleN + 1
        Set FSdestFolder = FSO.GetFolder(yearSubfolder & "SALE" & saleN)
    Wend
   
    'Move files from the source folder to the current SALE<n> subfolder
   
    For Each FSfile In FSsourceFolder.Files
       
        'Create the next SALE<n> subfolder if the maximum number of files in the current FSdestFolder has been reached
       
        If Not FSdestFolder Is Nothing Then
            If FSdestFolder.Files.Count >= MAX_FILES_PER_SUBFOLDER Then Set FSdestFolder = Nothing
        End If
           
        If FSdestFolder Is Nothing Then
            saleN = saleN + 1
            Set FSdestFolder = FSO.CreateFolder(yearSubfolder & "SALE" & saleN)
        End If
           
        If FSO.FileExists(FSdestFolder.Path & "\" & FSfile.Name) Then FSO.DeleteFile FSdestFolder.Path & "\" & FSfile.Name, True
        FSfile.Move FSdestFolder.Path & "\"
   
    Next

    MsgBox "Done"
   
End Sub
 
Upvote 0
Solution
what do you mean select?
as I said creating folders automatically

currently 100 different extensions files and will increase .

I search for macro does it automatically without interfer from me
So, for example, do you want 2023 files in the SALES_2023 folders? If so then is it only the file created or modified date that indicates which year it belongs to?
 
Upvote 0
@John_w
about this
when put the 30 files into folder should be from old date to new date .
I've found this sentences doesn't make sense , sorry !
your macro works perfectly as waht I want, just curiosity if I have folders & subfolders contain files in directory C:\Sales , should also move files from them to created folders .
I know to don't mentioned that, sorry ! but I want to be the macro is flexible instead of come back to ask adjusting the code again , may be theses cases occurs in the future .

thanks again
 
Upvote 0
if I have folders & subfolders contain files in directory C:\Sales , should also move files from them to created folders

This would require major changes to the code, for example the For Each FSfile loop moved to a recursive procedure which loops through files in a folder and calls itself for each subfolder. It should also ignore the C:\Sales\SALES_2022 folder and its subfolders. There are many examples of recursive FileSystemObject procedures on this forum.
 
Upvote 0
This would require major changes to the code
um! I thought just some lines can mod them.

anyway this is not big problem , thanks very much;)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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