Vbscript - 比较并复制文件夹中的文件(如果比目标文件新)

发布于 2024-09-02 01:23:25 字数 816 浏览 5 评论 0原文

我正在尝试设计这个脚本,该脚本应该用作很多用户的登录脚本的一部分。该脚本基本上应该采用源文件夹和目标文件夹,因为基本上只是确保目标文件夹具有与源文件夹完全相同的内容。但仅当源文件的修改日期标记比目标文件新时才复制。

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

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

这行得通吗?我不太确定如何做到这一点,但我可能会花一整天的时间来弄清楚。但这里的人通常都非常聪明,在你的帮助下,花费的时间会少得多:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-09-09 01:23:25

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

稍微修改了 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。


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

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.

Slightly modified examples from 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

Edit: Misspelled the URL.


Improvement
In the comment, "date1" and "date2" have been exchanged.
The MSDN document says:
If date1 refers to a later time than date2, the DateDiff function returns a negative number.
http://msdn.microsoft.com/en-us/library/xhtyw595(v=vs.84).aspx

风苍溪 2024-09-09 01:23:25

你的代码看起来很合理。只需留意只读文件等。

您可以使用FileSystemObject来执行实际的文件操作,只需查看:

http://msdn.microsoft.com/en-us/library/6kxy1a51%28VS.85%29.aspx

Your code looks reasonable. Just look out for readonly files and such.

You can use the FileSystemObject to do the actual file operations, just look at:

http://msdn.microsoft.com/en-us/library/6kxy1a51%28VS.85%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文