?
Solved

How to delete a folder

Posted on 2005-03-07
10
Medium Priority
?
761 Views
Last Modified: 2012-08-13
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
Comment
Question by:mark951
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13481406
what about

folder.delete True

at the end?
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13481415
or "fldr.Delete True" if you want to delete only subdirectories
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13481457
Maybe this:

    Dim sFolder As String
    sFolder = Folder.Path
    Set Folder = Nothing
    fso.DeleteFolder sFolder, True
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mark951
ID: 13481487
I only want to delete the folder if it is older than 3 days
0
 
LVL 22

Accepted Solution

by:
DarkoLord earned 2000 total points
ID: 13481589
If DateDiff("d", fldr.DateCreated, Now) > 3 Then
    fldr.Delete True
End If
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13481600
instead of DateCreated you can use DateLastModified
0
 

Author Comment

by:mark951
ID: 13481842
Is DateDiff a Method IN VB? or do I have to create it?
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13481849
It's built-in, so you don't have to create it..
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13481873
'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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13481951
'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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question