Deleting some files from a specific folder

Mirica_Victor

Registered User.
Local time
Tomorrow, 01:00
Joined
Oct 24, 2008
Messages
11
Hy,
I have a database wich on a button creates a *.rtf report in a folder. As I create a lot of files in the same folder, i wonder if I could control the number of files in that folder (each time I create a new report, the oldest one wil be deleted). For this I need to have a table or a query that contains the files in that folder and each time i create a new report the oldest one must be deleted (delete qry to del the min of date created *fso). Seems easy, but how do I get that table of files into access? this table wil be created each time I create a new report, and the del qry wil run also.
Any ideas?
 
I'd be inclined to go straight to the directory. You could store what was in the directory at that time in a table, but if someone goes into that directory and changes things, what's in the table doesn't match what's in the directory.

The File System Object is useful for this kind of file manipulation and might be worth doing a bit of reading up on.
 
I solved the problem. here is what I did:

I created a table wich contains the name of the report. Each time I export a new report, an append qry inserts the name in that table. (it also inserts the created date, but I do not use it)

I have this VBA:
Code:
DoCmd.OpenQuery "Add_Report" 'ads the curent report name to tha table
 
If DCount("Name", "Exported_Raps") > 10 Then 'cheks If the table has more than 10 files (including the last one added)
Dim Oldest_Report As String
Oldest_Report = DFirst("Name", "Exported_Raps")
Delete_Fis ("adress to file" & Oldest_Report & ".rtf")
DoCmd.OpenQuery "Sterg_Raport"  'deletes the oldest report
Else: End If

That solved my problem!

Delete_Fis:

Code:
Public Function Delete_Fis(file As String)
Dim fso As Object
    Dim f As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.GetFile(file)
            If fso.FileExists(file) Then
                fso.DeleteFile (file)
            End If
 
End Function
 

Users who are viewing this thread

Back
Top Bottom