Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

delete empty all of folders in device

0

Hello,

this code creates year folder and months folders inside year folder after that move files from folders and subfolders and sub-subflders are existed in ths device C:\Users\AMR\Desktop\CASES      to months folders and result of that become many folders are empty . so what I want after move files  should delete all of folders are empty

Option Explicit


Dim FSO As Scripting.FileSystemObject
Dim lngR As Long
Dim strExt As String
Dim objFile As Scripting.File
Dim objFolderSource As Scripting.Folder
Dim objFolderYear As Scripting.Folder
Dim objFolderMonth As Scripting.Folder
Dim dteMod As Date
Dim strPath As String
Dim strDir As String
Dim strYear As String
Dim fsoFile As Scripting.File
Dim fsoSFolder As Scripting.Folder


Sub MoveFilesIntoYearMonthFolders()

    strPath = "C:\Users\AMR\Desktop\CASES"
    Set FSO = New FileSystemObject
    Set objFolderSource = FSO.GetFolder(strPath)
    MoveFiles objFolderSource

End Sub

Sub MoveFiles(fsoPFolder As Scripting.Folder)

    For Each fsoFile In fsoPFolder.Files
        dteMod = fsoFile.DateLastModified
        strYear = strPath & "\cases-" & Year(dteMod)
        strDir = Dir(strYear, vbDirectory)
        If strDir = "" Then MkDir strPath & "\cases-" & Year(dteMod)
        Set objFolderYear = FSO.GetFolder(strPath & "\cases-" & Year(dteMod))
        strDir = Dir(objFolderYear.Path & "\" & Format(Month(dteMod), "00"), vbDirectory)
        If strDir = "" Then MkDir objFolderYear.Path & "\" & Format(Month(dteMod), "00")
        Set objFolderMonth = FSO.GetFolder(objFolderYear.Path & "\" & Format(Month(dteMod), "00"))
        FSO.MoveFile fsoPFolder.Path & "\" & fsoFile.Name, objFolderMonth.Path & "\" & fsoFile.Name
    Next fsoFile

    If fsoPFolder.SubFolders.Count = 0 And LCase(fsoPFolder.Path) <> LCase(strPath) Then
        fsoPFolder.Delete True
    Else
        For Each fsoSFolder In fsoPFolder.SubFolders
            If Not fsoSFolder.Name Like "cases*" Then MoveFiles fsoSFolder
        Next fsoSFolder
    End If

End Sub

thanks

Answer
Discuss

Discussion

Hi again Amer

I don't have time to solve this today but maybe tomorrow. Once the files have been moved to the year folders (e.g. "cases-2025"), do you want to delete any empty (=month) subfolders within those folders? Please clarify.
John_Ru (rep: 6762) Sep 14, '25 at 9:23 am
Hi John,
do you want to delete any empty (=month) subfolders within those folders? Please clarify.
surely not 
the olders folders are not months so delete any empty folder is not month and were existed before  create cases-2025  folder.

Amer omar (rep: 6) Sep 14, '25 at 10:13 am
Thanks Amer. I think I understand- please see my Answer and file.
John_Ru (rep: 6762) Sep 15, '25 at 9:30 am
Add to Discussion

Answers

0
Selected Answer

Amer

(Note for other users- this code needs the VBA Reference "Microsoft Scripting Runtime" to work.)

The existing code in MoveFilesIntoYearMonthFolders (below)- in conjunction with sub MoveFiles- moves all files in the stated directory into year folders and month subfolders based on file date information. That directory then has folders like "cases-2022", "cases-2025" etc. plus any folders which existed before the files were moved.

To delete the (now emptied) folders, the attached file adds the code in bold below (with explanatory comments): 

    Option Explicit

    Dim FSO As Scripting.FileSystemObject
    Dim lngR As Long
    Dim strExt As String
    Dim objFile As Scripting.File
    Dim objFolderSource As Scripting.Folder
    Dim objFolderYear As Scripting.Folder
    Dim objFolderMonth As Scripting.Folder
    Dim dteMod As Date
    Dim strPath As String
    Dim strDir As String
    Dim strYear As String
    Dim fsoFile As Scripting.File
    Dim fsoSFolder As Scripting.Folder


    Sub MoveFilesIntoYearMonthFolders()

    strPath = "C:\Users\AMR\Desktop\CASES"

    Set FSO = New FileSystemObject
    Set objFolderSource = FSO.GetFolder(strPath)
    MoveFiles objFolderSource

    ' After files have been moved, loop through at top level...
    For Each fsoSFolder In objFolderSource.SubFolders
        If Not fsoSFolder.Name Like "cases*" Then
            ' delete any remaining non-year folder (and any subfolders)
            fsoSFolder.Delete
        End If
    Next fsoSFolder

End Sub

Revison #1, 16 September 2025

In the revised file attached, the code above is modifed to use the FSO DeleteFolder method (which should delete the folder, its content and likewise for any level of subfolder), see change in bold in code extract below:

    ' After files have been moved, loop through at top level...
    For Each fsoSFolder In objFolderSource.SubFolders
        If Not fsoSFolder.Name Like "cases*" Then
            ' delete any remaining non-year folder (and any subfolders) including Read Only
            FSO.DeleteFolder fsoSFolder.Path, True
        End If
    Next fsoSFolder

Hope this fixes your problem, Amer. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

thanks,but doesn't delete sub-subfolders in directory , just delete folders are existed in directory ignoring subfolders within folders in directory.
Amer omar (rep: 6) Sep 16, '25 at 4:25 am
Amer. I'm confused again...

The code deletes the folders which existed (including any subfolders). What remains is the year folders (and any month subfolders required for the moved files).

Which other subfolders exist after the code has ran? Are they already within the year folders? 

(Note that you can't upload an image on this site but you could paste one into an Excel file and upload that under your Question (when editting that). 
John_Ru (rep: 6762) Sep 16, '25 at 6:03 am
there are no subfolders within year folder ,because there are no year folder from the basic before run the macro.

I have folders  before run the code like this
C:\Users\AMR\Desktop\CASES \report
C:\Users\AMR\Desktop\CASES \report\files
C:\Users\AMR\Desktop\CASES \report\files\output

the only folder delete REPORT folder  and keep FILES folder,OUTPUT folder after run the macro .
directory will be 
C:\Users\AMR\Desktop\CASES \cases-2025\01\
C:\Users\AMR\Desktop\CASES \cases-2025\02\
and no any older folders and subfolders and sub-subfolders within directory.
I hope this help.
Amer omar (rep: 6) Sep 16, '25 at 6:47 am
Amer, I don't see that problem here but please see Revison #1, 16 September 2025 to my Answer plus the new file.
John_Ru (rep: 6762) Sep 16, '25 at 7:51 am
I don't see that problem here 
my apologies !
now I see my bad when I tested .I added new folders within year folder , that's why doesn't delete folders. I should delete cases-2025 from the beginning to test the code.
thank you so much John.
Amer omar (rep: 6) Sep 16, '25 at 8:28 am
I thought that might be the case but happily you recognised it.

Thanks for selecting my Answer, Amer.
John_Ru (rep: 6762) Sep 16, '25 at 9:20 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login