Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

delete files older than 3 days of creation

Posted on 2011-03-08
9
Medium Priority
?
348 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 85
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

876 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