D
DavidH56
Hi,
I found the following code posted by Ron Debruin to delete all files except
most recent within a folder. What I'd like to do is to move all except the
most recent excel file from the current folder to the archives folder leaving
the most recent excel file. There may be more file types than excel within
the current folder.
Function NewestFile(Directory, FileSpec)
' John Walkenbach
' http://www.j-walk.com/ss/excel/tips/tip97.htm
' Returns the name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
Dim FileName As String
Dim MostRecentFile As String
Dim MostRecentDate As Date
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
FileName = Dir(Directory & FileSpec, 0)
If FileName <> "" Then
MostRecentFile = FileName
MostRecentDate = FileDateTime(Directory & FileName)
Do While FileName <> ""
If FileDateTime(Directory & FileName) > MostRecentDate Then
MostRecentFile = FileName
MostRecentDate = FileDateTime(Directory & FileName)
End If
FileName = Dir
Loop
End If
NewestFile = MostRecentFile
End Function
Sub Kill_All_Old_Files_in_Folder()
Dim Folder As String
Dim str1 As String
Dim str2 As String
Folder = "c:\Data\"
str1 = NewestFile(Folder, "*.xls")
str2 = Folder & str1
If str1 <> "" Then
Name str2 As Left(str2, Len(str2) - 4) & ".rdb"
On Error Resume Next
Kill Folder & "*.xls"
On Error GoTo 0
Name Left(str2, Len(str2) - 4) & ".rdb" As str2
End If
End Sub
Thanks in advance for your help.
I found the following code posted by Ron Debruin to delete all files except
most recent within a folder. What I'd like to do is to move all except the
most recent excel file from the current folder to the archives folder leaving
the most recent excel file. There may be more file types than excel within
the current folder.
Function NewestFile(Directory, FileSpec)
' John Walkenbach
' http://www.j-walk.com/ss/excel/tips/tip97.htm
' Returns the name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
Dim FileName As String
Dim MostRecentFile As String
Dim MostRecentDate As Date
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
FileName = Dir(Directory & FileSpec, 0)
If FileName <> "" Then
MostRecentFile = FileName
MostRecentDate = FileDateTime(Directory & FileName)
Do While FileName <> ""
If FileDateTime(Directory & FileName) > MostRecentDate Then
MostRecentFile = FileName
MostRecentDate = FileDateTime(Directory & FileName)
End If
FileName = Dir
Loop
End If
NewestFile = MostRecentFile
End Function
Sub Kill_All_Old_Files_in_Folder()
Dim Folder As String
Dim str1 As String
Dim str2 As String
Folder = "c:\Data\"
str1 = NewestFile(Folder, "*.xls")
str2 = Folder & str1
If str1 <> "" Then
Name str2 As Left(str2, Len(str2) - 4) & ".rdb"
On Error Resume Next
Kill Folder & "*.xls"
On Error GoTo 0
Name Left(str2, Len(str2) - 4) & ".rdb" As str2
End If
End Sub
Thanks in advance for your help.