Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

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
0
mark951
Asked:
mark951
  • 5
  • 2
  • 2
  • +1
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now