@user, I believe this is the functionality that you are looking for. I addressed the heart of your problem in Sub Find()
. Not guaranteed to be bug-free and I'm sure exception handling could be further improved.
Tip: See this answer for information on downloading Microsoft's authoritative WSH reference as a Windows help file.
Option Explicit
Dim oFF : Set oFF = New FileFinder
oFF.RootFolder = "C:\Source\Folder" ' absolute or relative path
oFF.DestinationFolder = "C:\Copy\Folder\" ' must end with backslash
On Error Resume Next
'
' Find the newest and second-newest files.
'
oFF.Find
If Err Then
WScript.Echo "Find error: " & Err.Description
WScript.Quit(1)
Else
WScript.Echo "Newest file: " & oFF.NewestFilePath
WScript.Echo "Second-newest file: " & oFF.SecondNewestFilePath
End If
'
' Copy the second-newest file to the destination folder.
'
oFF.CopySecondNewestFileToDestination
If Err Then
WScript.Echo "Copy error: " & Err.Description
WScript.Quit(1)
Else
WScript.Echo "'" & oFF.SecondNewestFilePath _
& "' was copied to folder '" & oFF.DestinationFolder & "'."
End If
Set oFF = Nothing
' ============================================================
Class FileFinder
' Public RootFolder
Public NewestFilePath
Public NewestFileDate
Public SecondNewestFilePath
Public SecondNewestFileDate
Private mFso
Private mRootFolder
Private mDestinationFolder
Private Sub Class_Initialize()
Set mFso = CreateObject("Scripting.FileSystemObject")
Me.SecondNewestFilePath = ""
Me.SecondNewestFileDate = CDate("1970/01/01")
Me.NewestFilePath = ""
Me.NewestFileDate = DateAdd("s", 1, Me.SecondNewestFileDate)
End Sub
Private Sub Class_Terminate()
Set mFso = Nothing
End Sub
Public Property Let RootFolder(sValue)
If Not mFso.FolderExists(sValue) Then
Err.Raise vbObjectError + 1, "", _
"Root folder '" & sValue & "' does not exist."
End If
mRootFolder = sValue
End Property
Public Property Get RootFolder()
RootFolder = mRootFolder
End Property
Public Property Let DestinationFolder(sValue)
If Not (Right(sValue, 1) = "\") Then
Err.Raise vbObjectError + 1, "", _
"Destination folder '" & sValue & "' must end with a backslash."
End If
If Not mFso.FolderExists(sValue) Then
Err.Raise vbObjectError + 1, "", _
"Destination folder '" & sValue & "' does not exist."
End If
mDestinationFolder = sValue
End Property
Public Property Get DestinationFolder()
DestinationFolder = mDestinationFolder
End Property
Public Sub Find()
Dim oFolder : Set oFolder = mFso.GetFolder(RootFolder)
Dim oSubFolder, oFile
For Each oSubFolder In oFolder.SubFolders
For Each oFile In oSubFolder.Files
'
' File is newer than newest file.
'
If DateDiff("s", NewestFileDate, _
oFile.DateLastModified) > 0 Then
SecondNewestFilePath = NewestFilePath
SecondNewestFileDate = NewestFileDate
NewestFilePath = oFile.Path
NewestFileDate = oFile.DateLastModified
'
' File is newer than second-newest file.
'
ElseIf DateDiff("s", SecondNewestFileDate, _
oFile.DateLastModified) > 0 Then
SecondNewestFilePath = oFile.Path
SecondNewestFileDate = oFile.DateLastModified
End If
Next
Next
End Sub
Public Sub CopySecondNewestFileToDestination()
mFso.CopyFile SecondNewestFilePath, DestinationFolder
End Sub
End Class