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
