How to kill multiple "old" files

A

azu_daioh

I'm trying to write code that will kill any other old files in the
same folder. Here's what i have so far:

Dim xPath As String
Dim xExt As String
Dim xFile As String
Dim xDate As Date
Dim xOldFileName As String



xPath = "\\Location\folder\myCompactDB"
xDate = Format(Date - 1, "mmddyyyy")
xFile = "*"
xExt = ".mdb"
xOldFileName = xPath & xDate & xFile & xExt

If xDate < Format(Date, "mmddyyyy") Then
Kill xOldFileName
End If
 
A

azu_daioh

oops. Hit send by mistake.

Anyway pls disgard the above code: Here's the code i have

Dim xPath As String
Dim xExt As String
Dim xFile As String
Dim xOldFileName As String

xPath = "\\Location\folder\myCompactDB"
xFile = "??????????????"
xExt = ".mdb"
xOldFileName = xPath & xFile & xExt

If xOldFileName <> strNewDBName Then
Kill xOldFileName
End If

-------
my folder contains the following compact/backup databases:
myCompactDB06252007021453
myCompactDB06252007051435
myCompactDB06262007014253
myCompactDB06282007144701

this is created by "myCompactDB" & Format(Date, "mmddyyyy") &
Format(xTime, "hhnnss")
where xTime = time
strNewDBName = is the name of my newly created myCompactDB followed by
a date stamp

I would like to write a code that will delete any files with a
previous date on it.
For example if today is 06/28/2007 when the code is run, it should
delete any files with dates prior to 06/28/2007.

The code above deletes everything because of the wildcard and I dont
know how to prevent this.
Thank you.
 
D

Daniel

This should get you started

Sub KillOldFiles()
Dim i As Long
With Application.FileSearch
.NewSearch
.LookIn = "C:\Backup"
.FileName = "*.*"
.FileType = msoFileTypeAllFiles
.PropertyTests.Add name:="Last Modified", _
Condition:=msoConditionOnOrBefore, _
Value:=DateAdd("m", -1, Date), _
Connector:=msoConnectorAnd
.Execute
For i = 1 To .FoundFiles.Count
Debug.Print .FoundFiles(i)
Next i
End With
End Sub

taken from
http://groups.google.ca/group/micro...read/thread/1cc03b6d7587f2ae/1050233d9392bc20
 
A

azu_daioh

Thanks Daniel

With the code above, i keep getting variable not defined error. And
it's highlighting the msoFileTypeAllFiles

Any ideas? I'm unable to follow the link you provided "websense" has
it on their filter list.
Thanks again.
 
A

azu_daioh

Nvm. I found out why. I had to turn on the Microsoft Office Code
Library from the reference window.


Thanks again.
 
Top