troubleshooting Question

Deleting folder and files with fso

Avatar of NUPE2006
NUPE2006 asked on
Visual Basic Classic
21 Comments1 Solution1117 ViewsLast Modified:
Hello all,

The code below works fine for deleting files but how can I, it at all possible, get it to delete folders as well? Also is there any way to get the message box to show the number of files/folders deleted?
Private Sub cmdYesD_Click()
    Dim File1 As File
    Dim FileCollection As Folder
    Dim fso As New FileSystemObject
    Dim strRegion2 As String
    Dim intDays2 As Integer
    strRegion2 = Me.txtRegionConf.Text
    intDays2 = Me.txtInputDays.Text
'Deletes Files
    If fso.FolderExists("Z:\" & strRegion2 & "\Customer\Output") Then
        Set FileCollection = fso.GetFolder("Z:\" & strRegion2 & "\Customer\Output")
        For Each File1 In FileCollection.Files
            If DateDiff("d", FileDateTime(File1), Now) >= intDays2 Then
                fso.DeleteFile (File1)
             MsgBox "Operation Complete"
'Reload combobox on main form
'Load Regions From File
    Dim the_file As String
    the_file = "C:\Documents and Settings\hbc1636\Desktop\regions.txt"
'open the file
    Open the_file For Input As #1
    While Not EOF(1)
'read in a line
    Line Input #1, temp
'add that line to the combobox
    If Len(temp) <> 0 Then Main.cboSelectRegion.AddItem temp
'close the file
    Close #1
'Hide current form and show main
        'MsgBox "No files fit range"
            End If
        MsgBox "Error: Region not Found"
    End If
End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 21 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 21 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros