• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Delete subfolder and files

ysherriff

Member
Hi

I found a macro where I can delete all files located in subfolders but for some reason this macro is not working. Attached is the image of the subfolders

Code:
Sub Clear_All_Files_And_SubFolders_In_Folder()
'Delete all files and subfolders
'Be sure that no file is open in the folder
  Dim FSO As Object
  Dim MyPath As String

  Set FSO = CreateObject("scripting.filesystemobject")

  MyPath = "S:\SPandA\Sales Operations\Workstreams\Logs\DirectConnect Summary Report\DC Team Member Logs"  '<< Change

  If Right(MyPath, 1) = "\" Then
  MyPath = Left(MyPath, Len(MyPath) - 1)
  End If

  If FSO.FolderExists(MyPath) = False Then
  MsgBox MyPath & " doesn't exist"
  Exit Sub
  End If

  On Error Resume Next
  'Delete files
  FSO.deletefile MyPath & "\*.*", True

What am I doing wrong? Thanks for your help.
upload_2016-3-21_17-21-7.png
End Sub
 
The code looks like it's only checking single folder and removing all files in that folder. There is no routine to check subfolders from what I can see. And it looks like partial code from Ron de Bruin's site. You are missing last part of the code.

See link (it's the bottom example given)
http://www.rondebruin.nl/win/s4/win004.htm

Now, what exactly do you need? Entire subfolders and it's contents deleted as well? Or just the files and not the folders? If it's just files from folder and subfolders... Ron's code isn't meant for that. His code will delete all contents and subfolders within the path specified.
 
Try this
Code:
Sub Clear_All_Files_And_SubFolders_In_Folder()
    Dim FSO As Object
    Dim MyPath As String
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    MyPath = "S:\SPandA\Sales Operations\Workstreams\Logs\DirectConnect Summary Report\DC Team Member Logs\"
   
    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If
   
    If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & " Doesn't Exist", 64
        Exit Sub
    End If
   
    On Error Resume Next
        FSO.DeleteFile MyPath & "\*.*", True
        FSO.DeleteFolder MyPath & "\*.*", True
    On Error GoTo 0
End Sub
 
Hi. I just need the contents of the subfolders deleted. let me try the code Yasser provided and see if it works. stay tuned!!

and thanks
 
Never mind
630242465.png
 
Thanks shrivallabha. I am going to try this code because the other code did not delete the files in the subfolders. It deleted the entire subfolders. I only need the files deleted in the subfolders. I will let you know if this works.
 
Back
Top