0

我遇到了麻烦,我需要在 VBScripting 中执行以下任务。提前感谢您的帮助。

我想将第二个最新修改的文​​件从一个文件夹复制到另一个位置。

例如:源文件夹“Final”将包含许多子文件夹。运行脚本后,它应该检查“Final”的所有子文件夹中第二个最新修改的文​​件,并将其复制到目标文件夹。

4

1 回答 1

0

@user,我相信这是您正在寻找的功能。我在Sub Find(). 不保证没有错误,我相信异常处理可以进一步改进。

提示:有关将 Microsoft 的权威 WSH 参考下载为 Windows 帮助文件的信息,请参阅此答案。

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
于 2012-08-18T02:52:47.873 回答