@user이 기능은 사용자가 찾고있는 기능이라고 생각합니다. 문제의 핵심은 Sub Find()
입니다. 버그가 없다는 보장은 없으며 예외 처리가 더욱 향상 될 수 있다고 확신합니다.
팁 : Microsoft의 신뢰할 수있는 WSH 참조를 Windows 도움말 파일로 다운로드하는 방법은 this answer을 참조하십시오.
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
문제를 일으킨 곳과 자신이 한 일을 알려주십시오. 지금 묻는 방법은 일종의 [ "이게 내 문제 야, 내게 코드를 제발!"] (http://slash7.com/2006/12/22/vampires/). – AutomatedChaos