Deleteing Folders in a Set location with a specific date format

JohnLee

Registered User.
Local time
Yesterday, 16:44
Joined
Mar 8, 2007
Messages
692
Hi,

I've had a search on the forum, but haven't been able to find anything that helps me work out what code I need to write to delete folders that will always appear in a set location for instance:

\\prdfs02\devimages\

The folders that sit under this will always have the following format as their name "yyyymmdd".

What I would like to be able to do is to write some code that will delete folders that are over 6 months old and all their contents.

The following is the code I have written so far, but have now got struck, any assistance would be greatfully received:

===========
BEGIN CODE
===========

Public Function Delete_Folders()

Dim FSO As FileSystemObject ' Declares the File System Object
Dim Folder As Folder 'Declares the Folder Object
Dim ImagePath ' Declares the ImagePath variable

' Set the image path here [the location where images are be stored]
ImagePath = "\\prdfs02\devimages\"

End Function

===========
END CODE
===========

John
 
Hi,

Here is what you need to add:

dim subFolder as Folder
dim dtDate as Date

Set Folder = FSO.GetFolder(ImagePath)

' That will get you the current date - 6 months
dtDate = DateSerial(Year(Date), Month(Date) - 6, Day(Date))

' Loops through sub-folders
For Each subFolder in Folder.Folders
' Compares the date with the name of the folder
If subFolder.Name <= Format(dtDate, "yyyymmdd") Then
FSO.Delete subFolder.Path
End If
Next

You might need to correct some syntax as I did that on top of my head, but that should give you an idea

Simon B.
 
Good morning Simonb,

Thanks very much for your response, I will let you know how I get on.

John
 
Hi Simonb,

I had to make some minor adjustments as your said and my code now looks like this:

================
BEGIN CODE
================

Dim FSO As FileSystemObject ' Declares the File System Object
Dim Folder As Folder 'Declares the Folder Object
Dim ImagePath 'Declares the ImagePath variable
Dim subFolder As Folder
Dim dtmDate As Date

'Set the image path here [the location where images are be stored]
ImagePath = "\\prdfs01\images\"

Set FSO = New FileSystemObject

Set Folder = FSO.GetFolder(ImagePath)
' That will get you the current date - 6 months
dtmDate = DateSerial(Year(Date), Month(Date) - 4, Day(Date))
' Loops through sub-folders
For Each subFolder In Folder.SubFolders
' Compares the date with the name of the folder
If subFolder.Name <= Format(dtmDate, "yyyymmdd") Then
FSO.DeleteFolder subFolder.Path
End If
Next

==============
END CODE
==============

However Access doesn't like the "FOS.DeleteFolder subFolder.Path" part, bI get the following error message:

"Run-time error '70' Permission denied"

and when I click on the "Debug" button it highlights this complete section of the code.

Your assistance would be most appreicated.

John
 
Hi Simonb,

I found out why "access was denied" it is because there are still files within the folders, from what I understand you can't delete a folder if it has files within it.

So the next step then is to delete the files within my folders, so could I engage your assistance again, all the files within the folders will always be tif files (.tif), so I guess I need to have a way of deleting these before I can delete the folders.

So do I need to employ the same method for files as for folders to acheive that?

John
 
Hi SimonB,

I'm having one of those days, your original code done the job completely, there is no need for me to make more complicated than it needs be. I just added after the "FSO.DeleteFolder subFolder.Path" line "On Error Resume Next", which prevents the error messages I was getting from poping up, because if there are no folders that meet the required criteria then of course there's nothing more to do.

John
 
Hi,

The same method would work but I would first try that:

FSO.DeleteFolder subFolder.Path & "\*.*"
FSO.DeleteFolder subFolder.Path

That should empty the folder before deleting it. I'm not sure if you need the \ though, you'll need to test it. If that doesn't work, adapt the loop for the folders to files.

Simon B.
 
Hi SimonB,

I made one more change, having done some more reading up, which was that I removed the "FSO.DeleteFolder subFolder.Path" line "On Error Resume Next", and replaced it with "SubFolder.Delete True", which works great.

Thanks once again for your assistance

John
 

Users who are viewing this thread

Back
Top Bottom