Solved

delete files older than 3 days of creation

Posted on 2011-03-08
9
327 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
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35074539
"FILES" i guess, the good part is the he received solution(s)for both situations.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now