How to delete a folder

This code deletes all files in a specific directoring and sub directories, older than 3 days.  It doesn't however delete the folder itself.  The folders are therefore empty.  How do I delete the folder itself

path = "c:\tmp"
killdate = date() - 3

arFiles = Array()
set fso = createobject("scripting.filesystemobject")

' Don't do the delete/rename while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Creating an array of the file names first to avoid this.
'
SelectFiles path, killdate, arFiles

for n = 0 to ubound(arFiles)
  set f = arFiles(n)
  '=================================================
  ' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
  '=================================================
  f.delete true
next

msgbox "Done"


sub SelectFiles(sPath,vKillDate,arFilesToKill)

  'select files to delete and add to array...
  '
  set folder = fso.getfolder(sPath)
  set files = folder.files

  for each file in files
    ' uses error trapping around access to the
    ' Date property just to be safe
    '
    on error resume Next
    dtcreated = file.datelastmodified
    on error goto 0
    if not isnull(dtcreated) Then
      if dtcreated < vKillDate then
        count = ubound(arFilesToKill) + 1
        redim preserve arFilesToKill(count)
        set arFilesToKill(count) = file
      end if
    end if
  next

' *****************************************
' enable section below to include subfoldes
'
  for each fldr in folder.subfolders
    SelectFiles fldr.path,vKillDate,arFilesToKill
  next

end sub

'!!End Copy
mark951Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DarkoLordCommented:
what about

folder.delete True

at the end?
0
DarkoLordCommented:
or "fldr.Delete True" if you want to delete only subdirectories
0
Erick37Commented:
Maybe this:

    Dim sFolder As String
    sFolder = Folder.Path
    Set Folder = Nothing
    fso.DeleteFolder sFolder, True
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mark951Author Commented:
I only want to delete the folder if it is older than 3 days
0
DarkoLordCommented:
If DateDiff("d", fldr.DateCreated, Now) > 3 Then
    fldr.Delete True
End If
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DarkoLordCommented:
instead of DateCreated you can use DateLastModified
0
mark951Author Commented:
Is DateDiff a Method IN VB? or do I have to create it?
0
DarkoLordCommented:
It's built-in, so you don't have to create it..
0
nffvrxqgrcfqvvcCommented:
'This is what you want to do.
' Add this to your Forms_Declerations

Private Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long

'add a command button to form1
'put the following code inside the command button
'this deletes a folder

RemoveDirectory "C:\foldername"

Cheers!
0
nffvrxqgrcfqvvcCommented:
'I forgot to mention if there are files in the folder you must do this first
'if you want to remove a directory without files use above code.
'if you want to remove a directory with files inside the folder use this code.
'

Option Explicit
Private Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long

Private Sub Command1_Click()

Dim myfolder As String
myfolder = "c:\test" 'location of you folder to delete
Call Kill(myfolder & "\*.*")

DoEvents

Call RemoveDirectory(myfolder)

End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.