且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

比较两个不同文件夹中的两个文件并将其替换为较新的

更新时间:2022-11-17 16:16:35

如果您想根据上次修改日期进行覆盖,则 File 对象具有您想要的属性:DateLastModified代码>.(您可以检查 File 对象的所有属性 此处.)

If you want to overwrite based on the last modified date, then the File object has the property you want: DateLastModified. (You can check all properties of the File object here.)

您已经可以访问源文件对象(您的代码的 Photo 变量),因此您只需要获取目标的文件对象.

You already have access to the source file objects (your code's Photo variable) so you just need to get the target's file object.

这样的事情应该可以工作:

Something like this should work:

Dim Photo
Dim targetFile, bmpTargetFilename, jpgTargetFilename

SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"

Set ObjPhoto = CreateObject("Scripting.FileSystemObject")

For Each Photo In ObjPhoto.GetFolder(SourceFolder).Files
    bmpTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))
    jpgTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Photo.Name)

    If ObjPhoto.FileExists(bmpTargetFilename) Then
        ' Get the target file object
        Set targetFile = ObjPhoto.GetFile(jpgTargetFilename)
        ' Now compare the last modified dates of both files
        If Photo.DateLastModified > targetFile.DateLastModified Then
            Photo.Copy jpgTargetFilename, True
        End If
    Else
        Photo.Copy jpgTargetFilename, True
    End If
Next

一些注意事项:

  • 您似乎正在检查 .BMP 文件是否存在,但正在复制 .JPG 文件,因此我使用两个变量进行了明确说明.
  • 我还假设您想比较 JPG 文件,因为这些是被复制的文件.