using a Batch file to delete external files

tranchemontaigne

Registered User.
Local time
Yesterday, 17:32
Joined
Aug 12, 2008
Messages
203
BACKGROUND:
I have an external batch file that is called by MS Access. This backup file makes a copy of the calling MS Access file every time the database is opened.

GOAL:
What I want to do is now modify the batch file to delete old backups.

NAMING CONVENTION:
All bacup files are uncompressed and follow the naming convention below

YYYY-MM-DD_databaseName.mdb.bak

Any thoughts on how to delete all files where the YYYY-MM-DD is more than 7 days old?
________
F50
 
Last edited:
Here is an old function I use to purge outdated import files...

This is how you call the DeleteOldFiles function to delete files over 7 days old...
Code:
Call DeleteOldFiles(7, "X:\Your Old Files\")

Code:
Public Function DeleteOldFiles(iDaysOld As Integer, sDirectory As String)
On Error GoTo Err_DeleteOldFiles
'Must set a reference to the Microsoft Scripting Runtime

    Dim oFileSystem As FileSystemObject
    Dim oFolder As Folder
    Dim oFileCollection As Files
    Dim oFile As File
    
    'Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    Set oFileSystem = New FileSystemObject
    Set oFolder = oFileSystem.GetFolder(sDirectory)
    Set oFileCollection = oFolder.Files
    
    For Each oFile In oFileCollection
        If oFile.DateLastModified < (Date - iDaysOld) Then
            oFile.Delete (True)
        End If
    Next
    
    Set oFileSystem = Nothing
    Set oFolder = Nothing
    Set oFileCollection = Nothing
    Set oFile = Nothing

Exit_DeleteOldFiles:
    Exit Function

Err_DeleteOldFiles:
    If Err.Number = 76 Then 'Path not found.  User does not have access to the files for deletion.
        MsgBox Err.Number & " - " & Err.Description, vbCritical, "DeleteOldFiles()"
        Exit Function
    ElseIf Err.Number = 91 Then 'Object variable or With block variable not set.  User does not have access to the files for deletion.
        Exit Function
    ElseIf Err.Number = 424 Then 'Object required.  User does not have access to the files for deletion.
        Exit Function
    Else
        MsgBox Err.Number & " - " & Err.Description, vbCritical, "DeleteOldFiles()"
        Resume Exit_DeleteOldFiles
    End If

End Function

HTH
 
I cannot take all the credit for that for I found that code years ago somewhere in cyberspace or I might have asked a simular question myself when I had the need to delete old files.
 
Thanks for the follow up - it's a shame that such a useful function has outlived it's attribution.

I'm glad you were able to retrieve the code so quickly.
________
EASY VAPE REVIEW
 
Last edited:
Oh I still use that function in most of my production databases that have data file import functions.
 

Users who are viewing this thread

Back
Top Bottom