Vbscript - 检查每个子文件夹中的文件和复制文件
我正在努力让这个脚本发挥作用。 它基本上应该镜像两组文件夹并确保它们完全相同。如果文件夹丢失,则应复制该文件夹及其内容。
然后,脚本应比较 DateModified 属性,并且仅在源文件比目标文件新时才复制文件。
我正在尝试编写一个完全可以做到这一点的脚本。到目前为止,我已经能够检查所有子文件夹是否存在,如果不存在则创建它们。 然后,我能够扫描顶部源文件夹中的文件,如果它们不存在或者源文件上的 DateModified 属性较新,则复制它们。
剩下的基本上就是扫描每个子文件夹中的文件,如果它们不存在或者 DateModified 标记较新,则复制它们。
这是代码:
Dim strSourceFolder, strDestFolder
strSourceFolder = "c:\users\vegsan\desktop\Source\"
strDestFolder = "c:\users\vegsan\desktop\Dest\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTopFolder = fso.GetFolder(strSourceFolder)
Set colTopFiles = objTopFolder.Files
'Check to see if subfolders actually exist. Create if they don't
Set objColFolders = objTopFolder.SubFolders
For Each subFolder in objColFolders
CheckFolder subFolder, strSourceFolder, strDestFolder
Next
' Check all files in first top folder
For Each objFile in colTopFiles
CheckFiles objFile, strSourceFolder, strDestFolder
Next
Sub CheckFolder (strSubFolder, strSourceFolder, strDestFolder)
Set fso = CreateObject("Scripting.FileSystemObject")
Dim folderName, aSplit
aSplit = Split (strSubFolder, "\")
UBound (aSplit)
If UBound (aSplit) > 1 Then
folderName = aSplit(UBound(aSplit))
folderName = strDestFolder & folderName
End if
If Not fso.FolderExists(folderName) Then
fso.CreateFolder(folderName)
End if
End Sub
Sub CheckFiles (file, SourceFolder, DestFolder)
Set fso = CreateObject("Scripting.FileSystemObject")
Dim DateModified
DateModified = file.DateLastModified
ReplaceIfNewer file, DateMofidied, SourceFolder, DestFolder
End Sub
Sub ReplaceIfNewer (sourceFile, DateModified, SourceFolder, DestFolder)
Const OVERWRITE_EXISTING = True
Dim fso, objFolder, colFiles, sourceFileName, destFileName
Dim DestDateModified, objDestFile
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFileName = fso.GetFileName(sourceFile)
destFileName = DestFolder & sourceFileName
if Not fso.FileExists(destFileName) Then
fso.CopyFile sourceFile, destFileName
End if
if fso.FileExists(destFileName) Then
Set objDestFile = fso.GetFile(destFileName)
DestDateModified = objDestFile.DateLastModified
if DateModified <> DestDateModified Then
fso.CopyFile sourceFile, destFileName
End if
End if
End Sub
I'm trying to get this script to work.
It's basically supposed to mirror two sets of folders and make sure they are exactly the same. If a folder is missing, the folder and it's content should be copied.
Then the script should compare the DateModified attribute and only copy the files if the source file is newer than the destination file.
I'm trying to get together a script that does exactly that. And so far I've been able to check all subfolder if they exist and then create them if they don't.
Then I've been able to scan the top source folder for it's files and copy them if they don't exist or if the DateModified attribute is newer on the source file.
What remains is basically scanning each subfolder for its files and copy them if they don't exist or if the DateModified stamp is newer.
Here's the code:
Dim strSourceFolder, strDestFolder
strSourceFolder = "c:\users\vegsan\desktop\Source\"
strDestFolder = "c:\users\vegsan\desktop\Dest\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTopFolder = fso.GetFolder(strSourceFolder)
Set colTopFiles = objTopFolder.Files
'Check to see if subfolders actually exist. Create if they don't
Set objColFolders = objTopFolder.SubFolders
For Each subFolder in objColFolders
CheckFolder subFolder, strSourceFolder, strDestFolder
Next
' Check all files in first top folder
For Each objFile in colTopFiles
CheckFiles objFile, strSourceFolder, strDestFolder
Next
Sub CheckFolder (strSubFolder, strSourceFolder, strDestFolder)
Set fso = CreateObject("Scripting.FileSystemObject")
Dim folderName, aSplit
aSplit = Split (strSubFolder, "\")
UBound (aSplit)
If UBound (aSplit) > 1 Then
folderName = aSplit(UBound(aSplit))
folderName = strDestFolder & folderName
End if
If Not fso.FolderExists(folderName) Then
fso.CreateFolder(folderName)
End if
End Sub
Sub CheckFiles (file, SourceFolder, DestFolder)
Set fso = CreateObject("Scripting.FileSystemObject")
Dim DateModified
DateModified = file.DateLastModified
ReplaceIfNewer file, DateMofidied, SourceFolder, DestFolder
End Sub
Sub ReplaceIfNewer (sourceFile, DateModified, SourceFolder, DestFolder)
Const OVERWRITE_EXISTING = True
Dim fso, objFolder, colFiles, sourceFileName, destFileName
Dim DestDateModified, objDestFile
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFileName = fso.GetFileName(sourceFile)
destFileName = DestFolder & sourceFileName
if Not fso.FileExists(destFileName) Then
fso.CopyFile sourceFile, destFileName
End if
if fso.FileExists(destFileName) Then
Set objDestFile = fso.GetFile(destFileName)
DestDateModified = objDestFile.DateLastModified
if DateModified <> DestDateModified Then
fso.CopyFile sourceFile, destFileName
End if
End if
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这是一篇旧帖子,但我一直在寻找一种方法来运行 VBS 以根据修改日期复制和备份数据,并运行所有子目录和文件,并偶然发现了基于上述问题的解决方案,
您的代码有错误在该行中,
您的 DateModified 拼写错误,导致该行无法通过您的 file.datelastmodified 发送到您的子系统。除此之外,一旦我修复了它,您的代码就会复制第一层文件和文件夹。
我在此代码的基础上构建了复制多个级别的子目录,并通过每次使用动态数组在其内部再次调用子目录来复制每个 corespondng 子目录中的文件。
这组代码将比较两个文件并用新文件替换旧文件。
参见代码:
I know this is an old post but I have been looking for a way to run VBS to copy and backup data based on date modified and run through all sub directories and files and stumbled across a solution based on the above question
your code has an error in the line
you have DateModified miss-spelled causing this to not send through your file.datelastmodified on to your sub. Other then that your code was copying the first levels of files and folders once I repaired that.
I have built on this code to copy multiple levels of subdirectories and copy files in each corespondng subdirectory by calling the sub again within itself renaming the source folder everytime with a dynamic array.
This set of code will compare the two files and replace the older with the newer.
see code:
我确信这段代码令人愉快,但同步两个文件夹是一个常见问题,Windows 中包含免费实用程序可以做到这一点,因此您无需编写和维护此代码。 ROBOCOPY 是一个很好的起点。另请参阅 XCOPY 或开源替代方案,例如 rsync。
I'm sure this code is delightful, but syncing two folders is a common problem and there are free utilities included with Windows that will do it so you don't need to write and maintain this code. ROBOCOPY is a good place to start. See also XCOPY or open source alternatives such as rsync.