Deleting oldest file (1 Viewer)

jrjr

A work in progress
Local time
Today, 06:04
Joined
Jul 23, 2004
Messages
291
Greetings!

I am using this in a module to copy files to a directory:

----------------------------
Public Function Backup()

Dim fso As FileSystemObject

Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String

sSourcePath = "c:\a\"
sSourceFile = "db2.mdb"
sBackupPath = "c:\aa\"
sBackupFile = "BackupDB_" & Format(Date, "mmddyyyy") & "_" & Format(Time, "hhmmss") & ".mdb"

Set fso = New FileSystemObject
fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True
Set fso = Nothing

End Function

---------------------

Now, what I want to do is delete the oldest file after there have been 5 files copied. In other words, I will be running this once a day and only want to retain the newest 5 copies. Once the sixth has been copied, delete the oldest one in the directory.

Any suggestions?
 

jrjr

A work in progress
Local time
Today, 06:04
Joined
Jul 23, 2004
Messages
291
I decided to get it done with a batch and here is what I came up with:

@echo off

md c:\aa\now

rmdir c:\aa\6daysold /s /q

xcopy /e "c:\a" "C:\aa\now\"

rename c:\aa\5daysold 6daysold

rename c:\aa\4daysold 5daysold

rename c:\aa\3daysold 4daysold

rename c:\aa\2daysold 3daysold

rename c:\aa\yesterday 2daysold

rename c:\aa\current yesterday

rename c:\aa\now current
 

jrjr

A work in progress
Local time
Today, 06:04
Joined
Jul 23, 2004
Messages
291
Yes it should :) I believe it was part of your backup and zip function, without the zip part. Never could get that to work due to having the evaluation version of winzip. Thanks for the link, I did search but didn't find that. I will check it out.
 

Users who are viewing this thread

Top Bottom