Option Explicit
Public Sub Copy_Txt_Files_Except_Newest()
Dim sourceFolder As String, destinationFolder As String
Dim file As String
Dim newestFile As String, newestDate As Date
sourceFolder = "C:\path\to\folder1\"
destinationFolder = "C:\path\to\folder2\"
If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
newestDate = 0
newestFile = ""
file = Dir(sourceFolder & "*.txt")
While file <> vbNullString
FileCopy sourceFolder & file, destinationFolder & file
If FileDateTime(sourceFolder & file) > newestDate Then
newestFile = file
newestDate = FileDateTime(sourceFolder & file)
End If
file = Dir
Wend
If newestFile <> "" Then
Kill destinationFolder & newestFile
End If
End Sub
Public Sub Move_Txt_Files_Except_Newest()
Dim sourceFolder As String, destinationFolder As String
Dim file As String
Dim newestFile As String, newestDate As Date
sourceFolder = "C:\path\to\folder1\"
destinationFolder = "C:\path\to\folder2\"
If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
newestDate = 0
newestFile = ""
file = Dir(sourceFolder & "*.txt")
While file <> vbNullString
If FileDateTime(sourceFolder & file) > newestDate Then
newestFile = file
newestDate = FileDateTime(sourceFolder & file)
End If
Name sourceFolder & file As destinationFolder & file
file = Dir
Wend
If newestFile <> "" Then
Name destinationFolder & newestFile As sourceFolder & newestFile
End If
End Sub