Solved

delete files older than 3 days of creation

Posted on 2011-03-08
9
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error 438 6 50
Access lists formating 8 47
MS Access query 16 58
Access 2013 - User getting error when exporting preview reports from their machines. 18 42
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

739 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