Sub Clear_All_Files_SubFolders_In_Folder()
Dim FSO As Object
Dim MyPath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
MyPath = "C:\pull\"
If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If
If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " Folder Doesn't Exist"
Exit Sub
End If
On Error Resume Next
'Delete Files
FSO.DeleteFile MyPath & "\Backup.*", True
'Delete SubFolders
FSO.DeleteFolder MyPath & "\backup.*", True
End Sub
Sub Clear_All_Files_SubFolders_In_Folder()
Dim FSO As Object
Dim MyPath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
MyPath = "C:\test\"
If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If
If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " Folder Doesn't Exist"
Exit Sub
End If
On Error Resume Next
'Delete Files
FSO.DeleteFile MyPath & "\*Backup.*", True
Delete SubFolders
FSO.DeleteFolder MyPath & "\*.*", True
End Sub
Option Explicit
Option Compare Text
Sub DeleteBackupFiles(ByVal FolderPath As String)
' Author: Leith Ross
Dim File As Object
Dim Folder As Object
Dim FSO As Object
Dim SubFolder As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(FolderPath)
For Each File In Folder.Files
If File.Name Like "*backup.*" Then
File.Delete
End If
Next File
For Each SubFolder In Folder.SubFolders
DeleteBackupFiles SubFolder
Next SubFolder
End Sub
Sub Run()
DeleteBackupFiles "C:\test"
End Sub
Sub Delete()
Dim x, fldr As FileDialog, SelFold As String, i As Long
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
'Select Folder
With fldr
.Title = "Select a Folder"
If .Show <> -1 Then Exit Sub
SelFold = .SelectedItems(1)
End With
'Put all required filesinto an array
x = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & SelFold & "\Backup*.xls"" /s/b").stdout.readall, vbCrLf)
'Loop through the array and delete each file
For i = LBound(x) To UBound(x) - 1
CreateObject("scripting.filesystemobject").DeleteFile x(i)
Next i
'Show Result
MsgBox "The Following Files Were Deleted:" & vbLf & vbLf & Join(x, vbLf)
End Sub