Link to home
Start Free TrialLog in
Avatar of ahammar
ahammarFlag for United States of America

asked on

Deleting all files over a certain age in 1 folder

Hello fellow experts!

I have an Access application used as a front end, but I think my question is more of a VB question.  The frontend Access database is linked to a backend database.  I have code in the front end that makes a copy of the backend database and saves it in a backup folder in the on load event of the Startup form.  It also makes a backup before it closes and another in between when the user clicks a certain button.  I have a function that uses the current date and time along with a couple other things to come up with a name for the backups, so each backup has a different name.  They are all saved in the same folder.
My question is what is the code to search a folder and delete every file that is over a week old (or a month or whatever I choose) by checking the date the file was last modied.

I think it will take some API's but I'm not sure.  I just don't want to take the time to figure it out myself.  I would like some code that is already to go (except with whatever small changes I need to make)

You can use this example if you want:
Using todays date:
 (9/21/00)
I want to delete every file in this folder:
 "C:\Data\BkUps"
That is over 1 week old:
 (modified before 9/14/00)

I am using Win95 and Access97 but will soon be using Win2000 and Office2000.  If the code for the 2000 software is different (because I know some of the 95 API's don't work with 2000) and you don't know both, then I will take either, but let me know which it is.

I hope these points are enough for the complete code and make up for me being to lazy today to figure it out myself.

Cheers!
ahammar
Avatar of q2eddie
q2eddie
Flag of United States of America image

Hi, ahammar.

#Try This
1. Use the dir and the filedatetime functions.
(I tested this in Word97.)

' Display the names in C:\ that represent directories.
mypath = "c:\Data\bkups\"  ' Set the path.
myname = Dir(mypath)
Do While myname <> ""   ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If myname <> "." And myname <> ".." Then
        If FileDateTime(mypath & myname) < #9/14/00# Then
            Kill mypath & myname
        End If
    End If
    myname = Dir    ' Get next entry.
Loop

Bye. -e2
ASKER CERTIFIED SOLUTION
Avatar of q2eddie
q2eddie
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Create a reference to the Microsoft Scripting Runtime
and then use the following code:

    Dim fso As Scripting.FileSystemObject
    Dim fld As Folder
    Dim f   As File

    Dim CutOffDate As Date
   
    CutOffDate = DateAdd("d", -7, Now)
    'Where the -7 represents 7 days ago
   
    Set fso = New FileSystemObject
     
    Set fld = fso.GetFolder("C:\Data\BkUps")

    For Each f In fld.Files
        If f.DateCreated <= CutOffDate Then
            Debug.Print f.Name
            f.Delete
        End If
    Next



Hope this helps.

-M
Avatar of DennisBorg
DennisBorg

ahammar:

You could key off of the file name itself. Except you didn't tell us exactly how you named your files, only mentioning that the current date and time, among other things, are included in the file name.

The following routine will remove all files x number of days old.

This solution does *not* require adding any references to your project. It does not require learning WinAPI functions. Just simple VB code. Other library references and WinAPI are very useful tools, but they should never be used soley for the sake of using them. In some cases, native VB code is the best way to go.

My sample code would be used in the following manner:

   KillOldFiles "C:\Data\BkUps"  ' <--- deletes all files in that directory older than 7 days
   KillOldFiles "C:\Data\BkUps", 30 '<--- deletes all files in that directory older than 30 days

Add the following subroutine to a code module (.BAS). This routine will kill all files in the specified directory that was last modified over x days ago:

Public Sub KillOldFiles(ByVal sDirectory As String, Optional ByVal NumDaysOld As Integer = 7)
   Dim CurFile As String
   Dim ModDate As Date
   
   If Right(sDirectory, 1) <> "\" Then sDirectory = sDirectory & "\"
   CurFile = Dir(sDirectory & "*.*")
   Do While Len(CurFile)
      'Time Component of File's date is ignored
      ModDate = CLng(FileDateTime(sDirectory & CurFile))
      If ModDate + NumDaysOld < Date Then
         Kill sDirectory & CurFile
      End If
     
      CurFile = Dir()
   Loop
End Sub
Avatar of ahammar

ASKER

Thank you all for your replies!

q2eddie was the first to answer and it works fine for me, so I must give him the points.

Thanks again to all who replied and sorry you all couldn't have got the points.

I'll probably be back with more questions....

Cheers!
ahammar
To MELeBlanc and DennisBorg:

Your code is excellent!  It's obvious that you both put some thought into your responses.

Bye. -e2
Thank-you, q2eddie!  Congrats on your solution!