日期比较并仅复制新文件

时间:2017-03-01 09:17:48

标签: file date vbscript comparison backup

早上好, 我是编程代码的初学者,所以如果我做错了什么,我很抱歉。 我在VBS中编写了一个代码,用于备份从文件夹到另一个文件夹的一些文件。 我的问题是比较两个文件夹中的文件日期,并仅在文件是新的或日期已更改时才允许复制。 在这里我的代码,有人可以帮我找到问题吗? 我试过了但是没有用

    ' Copy a Folder


'Const OverWriteFiles = False
Dim strSourceFolder, strDestFolder
strSourceFolder = "E:\test1"
strDestFolder = "C:\test1"


Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "strSourceFolder" , "strDestFolder" 

For each file in StrSourceFolder
     ReplaceIfNewer ("file, strDestFolder")
Next

Sub ReplaceIfNewer (SourceFile, DestFolder)

    Dim DateModifiedSourceFile, DateModifiedDestFile
    DateModifiedSourceFile = SourceFile.DateModified()
    DateModifiedDestFile = DestFolder & "\" & SourceFile.DateModified()

    If DateModifiedSourceFile < DateModifiedDestFile then
        Copy SourceFile to SourceFolder Else
        End If


' Verify that a Folder Exists


'Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists("strDestFolder") Then  
MsgBox  "Backup Copy Done." & vbCrLf & (Day(Now) & "\" & Month(Now) & "\" & Year(Now)) , Vbinformation
    Set objFolder = objFSO.GetFolder("strDestFolder")
Else


MsgBox  "Folder does not exist." , vbCritical , "Folder does not exist." 



End if

谢谢并耐心等待!

1 个答案:

答案 0 :(得分:0)

.DateModified不是VBScript。开始阅读here。有DateDiff,但由于日期是双打,所以与<的比较也会有效。在代码中:

>> Set f = CreateObject("Scripting.FileSystemObject").GetFile(WScript.ScriptFullName)
>> dlm = f.DateLastModified
>> WScript.Echo TypeName(dlm), dlm, "(german locale)"
>> dlmn = DateAdd("s", 2, dlm)
>> WScript.Echo TypeName(dlmn), dlmn, "(german locale)"
>> WScript.Echo DateDiff("s", dlmn, dlm), DateDiff("s", dlm, dlmn), CStr(dlm < dlmn)
>> WScript.Echo CDbl(dlm)
>> WScript.Echo CDbl(dlmn)
>>
Date 22.11.2013 13:09:53 (german locale)
Date 22.11.2013 13:09:55 (german locale)
-2 2 Wahr
41600,5485300926
41600,5485532407