Solved

delete files older than 3 days of creation

Posted on 2011-03-08
9
336 Views
Last Modified: 2012-05-11
hi there,
I need to see use code on MS Access to delete files older than 3 days of creation base on date time created.
Whats the best solution for this task at hand?
Thanks,
COHFL
0
Comment
Question by:COHFL
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 84
ID: 35074493
What do you mean by "files"? Are you referring to files stored on disc, or are you referring to records in an Access table?

0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35074496
Open the cmd window, go to the folder you want to delete the files from and execute the following command:
forfiles -d -7 -c "cmd /c dir @file"
-- or --
forfiles -d -7 -c "cmd /c dir @path"

This will show you the files you want to delete. To actually delete them, do this:
forfiles -d -7 -c "cmd /c del @file"

The -7 is exactly 7 24 hour periods. So if it is 5:00pm and you want to delete a file from 6:00pm 7 days ago, it won't find it. It will find files before 5:00pm 7 days ago.

I highly suggest lookin at the help also by using the command: forfiles /?

Once you get what you want, you can put it in your own bat file also.
0
 
LVL 2

Expert Comment

by:chris_smith_51
ID: 35074514
Hi COHFL,

Sorry to be a pain, but do you mean to delete *records* in the database which are older than 3 days (files are something different!)?

...In which case a simple SQL DELETE query should do the trick:

DELETE FROM tbl_name WHERE creation_date < getdate()-3;

You can implement this in SQL Query Design View located through the wizard in Access.

Chris

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35074539
"FILES" i guess, the good part is the he received solution(s)for both situations.
0
 
LVL 84
ID: 35074591
The author asked for CODE to do this, not commands to manage this by hand. While a batch file could be used, of course, your solution wouldn't really be Access-friendly.
0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35074661
I appreciate your remarks but i suppose we are both trying to help out this person. It's noone's fault that people do not provide the exact information that we need in order to help them correctly. Ss i said in my previous post, in this case it's better to have multiple answers that solve one problem than having multiple unanswered questions.

Have a sweet day.
0
 

Author Comment

by:COHFL
ID: 35082618
I'm sorry for the confusion my question has cause. What im trying to do is to have a routine that went it gets call it goes out and the following:

Private Sub cmdDeleteFiles_Click()
On Error GoTo Err_cmdDeleteFiles_Click

Dim Directory as string
Directory = "C:Windows\Folder\"

'now here i need to loop for all the files that are and stored in Directory once i have them compared them with datetime() and the file datetime creation  and if the day is > than 3 them delete it. otherwise just leave it alone.


Exit_cmdDeleteFiles_Click:
    Exit Sub

Err_cmdDeleteFiles_Click:
    MsgBox Err.Description
    Resume Exit_cmdDeleteFiles_Click
End Sub

I have done this on .Net but MS Access does not behave the same way hence my question.
Help will be greatly appreciated.
0
 

Accepted Solution

by:
COHFL earned 0 total points
ID: 35083401
perhaps this little function will do the job:
Function delete_old(ByVal Dir_Path As String) As String
Dim Masq_File As String
Dim File_Date As Integer
Dim FDate As Date

Dim MyMonth As String
Masq_File = Dir_Path & "\" & "*.*"
Dir_Result = Dir(Masq_File, vbNormal)
    While (Dir_Result <> "")
    On erro GoTo BadName
    FDate = FileDateTime(Dir_Path & "\" & Dir_Result)
        If (FDate < DateAdd("d", -5, Now())) Then
            Kill Dir_Path & "\" & Dir_Result
        End If
BadName:
    Dir_Result = Dir
    Wend
End Function
0
 

Author Closing Comment

by:COHFL
ID: 35126283
it did what i really needed to do
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

740 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