比较两个不同文件夹中的两个文件并将其替换为较新的 [英] Compare two files in two different folders and replace it by the newer
问题描述
使用此 VBScript 代码,我可以复制文件.如果文件存在它什么都不做,否则它会复制需要的文件.
With this VBScript code I was able to copy files. If the file exists it does nothing, if not it will copy the files needed.
Dim Photo
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder( SourceFolder).Files
If Not ObjPhoto.FileExists(ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))) Then
photo.Copy ObjPhoto.BuildPath(DistinationFolder, Photo.Name), True
End If
Next
如果源文件也存在于目标文件夹中,我想比较文件并将其替换为较新的.
I want to compare the files if the source files also exists in the destination folder and replace it by the newer.
推荐答案
如果您想根据上次修改日期进行覆盖,则 <code>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 文件,因为这些是被复制的文件.
这篇关于比较两个不同文件夹中的两个文件并将其替换为较新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!