Vbscript-比较和复制文件夹中的文件(如果比目标文件新) [英] Vbscript - Compare and copy files from folder if newer than destination files

查看:133
本文介绍了Vbscript-比较和复制文件夹中的文件(如果比目标文件新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计该脚本,该脚本应被用作许多用户的登录脚本的一部分。基本上,此脚本应该采用源文件夹和目标文件夹,因为基本上只需确保目标文件夹具有与源文件夹完全相同的内容即可。但是只有在源文件的日期修改后的戳记比目标文件新的情况下才进行复制。

I'm trying to design this script that's supposed to be used as a part of a logon script for alot of users. And this script is basically supposed to take a source folder and destination folder as basically just make sure that the destination folder has the exact same content as the source folder. But only copy if the datemodified stamp of the source file is newer than the destination file.

我一直在思考这个基本的伪代码,只是试图确保它基本有效且可靠。

I have been thinking out this basic pseudo code, just trying to make sure this is valid and solid basically.

Dim strSourceFolder, strDestFolder
strSourceFolder = "C:\Users\User\SourceFolder\"
strDestFolder = "C:\Users\User\DestFolder\"

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
        Copy SourceFile to SourceFolder
    End if

End Sub

这项工作吗?我不太确定该怎么做,但我可能会花一整天的时间来弄清楚。但是这里的人们通常是如此聪明,以至于在您的帮助下,所需的时间会大大减少:)

Would this work? I'm not quite sure how it can be done, but I could probably spend all day figuring it out. But the people here are generally so amazingly smart that with your help it would take alot less time :)

推荐答案

您的算法看起来不错。实际上,您将需要使用FileSystemObject获取这些文件,并检索它们各自的DateLastModified属性。您可以在两个日期上进行DateDiff比较,以比较早些。

Your algorithm looks good. Practically speaking, you would need to get the Files using a FileSystemObject, and retrieve their respective DateLastModified properties. You can do a DateDiff on the two dates to compare which is earlier.

来自 DevGuru

Dim filesys,demofile, date1, date2
Set filesys = CreateObject("Scripting.FileSystemObject")
Set demofile = filesys.GetFile("filename1")
date1 = demofile.DateLastModified
demofile = filesys.GetFile("filename2")
date2 = demofile.DateLastModified

If DateDiff("d", date1, date2) > 0 Then
    'date2 is more recent than date1, comparison by "day" ' ** Improvement **
End If

编辑:URL拼写错误。

Misspelled the URL.

改进
在评论中, date1和 date2已交换。
MSDN文档说:
如果date1引用的时间晚于date2,则DateDiff函数将返回负数。
http://msdn.microsoft .com / en-us / library / xhtyw595(v = vs.84).aspx

这篇关于Vbscript-比较和复制文件夹中的文件(如果比目标文件新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆