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

时间:2019-02-24 10:54:46

标签: vbscript

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

我创建了以下脚本:

letSum :: [Int] -> [Int]
letSum xs = [result | x <- xs, y <- xs, let result = x + y, result > 10]

normalSum  :: [Int] -> [Int]
normalSum xs = [x + y | x <- xs, y <- xs, x + y > 10]

现在我知道大多数importand行存在问题:

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.CopyFileobjFile.Path的打印内容当然是不同的。

1 个答案:

答案 0 :(得分:0)

实际上并不需要将文件的完整列表存储在内存中,而只是丢弃较新的文件。您可以简单地遍历文件列表,确保不复制较新的文件。

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