将所有新文件但最后修改的文件从位置 A 复制到位置 B

Copying all new files but last modified from location A to location B

我正在尝试创建一个 VBScript 来将每 X 分钟的文件从位置 A 复制到位置 B。 我的条件是:复制所有新文件(目标文件夹中不存在的文件)并且不复制最后修改的文件。 为此,我创建了一个列表,按上次修改日期对所有文件进行排序。

我创建了以下脚本:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim is_first
is_first = 1
Set list = CreateObject("ADOR.Recordset")
strOriginFolder = "C:\Users\Shelly\Desktop\test"
strDestinationFolder = "C:\Users\Shelly\Desktop\test2"
list.Fields.Append "name", 200, 255
list.Fields.Append "date", 7
list.Open

For Each f In objFSO.GetFolder(strOriginFolder).Files
    list.AddNew
    list("name").Value = f.Path
    list("date").Value = f.DateLastModified
    list.Update
Next
list.Sort = "date DESC"

list.MoveFirst
For Each objFile in objFSO.GetFolder(strOriginFolder).Files
    If is_first = 0 Then
        WScript.Echo list("date").Value & vbTab & list("name").Value
        WScript.Echo ("\n")
        WScript.Echo list("name").Value
        WScript.Echo ("\n")
        WScript.Echo objFile.Path
        If Not objFSO.FileExists(strDestinationFolder & "\" & list("name").Value) Then
            objFSO.CopyFile list("name").Value, strDestinationFolder & "\" &
            list("name").Value
        End If
    End If
    is_first = 0
    list.MoveNext
Next
list.Close

现在我知道最重要的一行有问题:

objFSO.CopyFile list("name").Value, strDestinationFolder & "\" & list("name").Value

但我不知道如何将 objFSO.CopyFile 与排序列表一起使用。 objFile.PathWScript.Echo list("name").Value 的打印当然是不同的。

没有必要为了丢弃较新的文件而将完整的文件列表存储在内存中。您可以简单地遍历文件列表,确保您不复制较新的文件。

Option Explicit

' Source and target folder configuration
Dim sourceFolderPath, targetFolderPath
    sourceFolderPath = ".\source"
    targetFolderPath = ".\target"

Dim targetFolder, testFile, newerFile, copyFile
    ' At the start there is not a new file nor a file to copy
    Set newerFile = Nothing 
    Set copyFile = Nothing 

    With WScript.CreateObject("Scripting.FileSystemObject")
        ' Get a full reference to target folder 
        targetFolder = .GetAbsolutePathName( targetFolderPath )
        ' Iterate over source file list
        For Each testFile In .GetFolder(.GetAbsolutePathName( sourceFolderPath )).Files
            ' Only process a file if it does not exist on target folder
            If Not .FileExists(.BuildPath( targetFolder, testFile.Name )) Then 
                If newerFile Is Nothing Then 
                    ' Is it the first file we see? Remember it as we still don't know 
                    ' if it is the newer one
                    Set newerFile = testFile 

                ElseIf testFile.DateLastModified > newerFile.DateLastModified Then 
                    ' We have found a file newer than the previously seen 
                    ' Select the previous one to copy and remember this new file
                    Set copyFile = newerFile
                    Set newerFile = testFile 

                Else 
                    ' Current file is not the newer one, copy it
                    Set copyFile = testFile 

                End If ' newerFile

                ' Is there a file to copy?
                If Not (copyFile Is Nothing) Then 
                    WScript.Echo "Copying " & copyFile.Path & " to " & .BuildPath( targetFolder, copyFile.Name ) 
                    copyFile.Copy .BuildPath( targetFolder, copyFile.Name )
                    Set copyFile = Nothing 
                End If ' copyFile

            End If ' FileExists
        Next ' testFile
    End With ' FileSystemObject