Option
Explicit
Dim
arPaths()
As
String
Dim
cntPaths
As
Integer
Sub
Aufruf()
FilesDelete
"C:\Hauptordner"
,
"Vormonat"
End
Sub
Sub
FilesDelete(RootPath
As
String
, DelPath
As
String
)
Dim
ObjFSO
As
Object
, ObjUPath
As
Object
Dim
searchPath
As
String
Dim
bFoundPath
As
Boolean
Dim
iPath
As
Integer
, iDelPath
As
Integer
Dim
DoDelPath
As
String
Dim
ObjFilename
As
Object
Set
ObjFSO = CreateObject(
"Scripting.FileSystemObject"
)
SearchUPaths RootPath
For
iPath = cntPaths - 1
To
0
Step
-1
If
Right(arPaths(iPath), Len(DelPath)) = DelPath
Then
DoDelPath = arPaths(iPath)
For
iDelPath = cntPaths - 1
To
0
Step
-1
If
Left(arPaths(iDelPath) +
"\", Len(DoDelPath + "
\
")) = DoDelPath + "
\"
Then
Debug.Print arPaths(iDelPath)
For
Each
ObjFilename
In
ObjFSO.GetFolder(arPaths(iDelPath)).Files
ObjFilename.Delete
True
Next
ObjFSO.GetFolder(arPaths(iDelPath)).Delete
True
End
If
Next
End
If
Next
cntPaths = 0
ReDim
arPaths(0)
End
Sub
Sub
SearchUPaths(Path
As
String
)
Dim
ObjFSO
As
Object
, ObjUPath
As
Object
Dim
searchPath
As
String
Dim
bFoundPath
As
Boolean
Set
ObjFSO = CreateObject(
"Scripting.FileSystemObject"
)
If
ObjFSO.FolderExists(Path)
Then
searchPath = Path
For
Each
ObjUPath
In
ObjFSO.GetFolder(searchPath).SubFolders
ReDim
Preserve
arPaths(cntPaths)
arPaths(cntPaths) = ObjUPath.Path
cntPaths = cntPaths + 1
SearchUPaths Path:=ObjUPath.Path
Next
End
If
End
Sub