Vba 复制文件夹和所有子文件夹,而不覆盖现有文件夹
0 我正在尝试使用fso.folder copy在网络驱动器上创建备份数据库。我的目的是移动文件夹中的所有文件和子文件夹,但如果备份驱动器上已存在文件,请跳过该文件,然后复制文件夹中的其余文件Vba 复制文件夹和所有子文件夹,而不覆盖现有文件夹,vba,database,directory,fso,Vba,Database,Directory,Fso,0 我正在尝试使用fso.folder copy在网络驱动器上创建备份数据库。我的目的是移动文件夹中的所有文件和子文件夹,但如果备份驱动器上已存在文件,请跳过该文件,然后复制文件夹中的其余文件 FSO.copyfolder Source:=Sourcefilename, Destination:=Destinfilename, OverwriteFiles:= False 但是,脚本在找到现有文件时会出错。任何建议都将不胜感激。请尝试下一个代码: Sub testCopyFolder() D
FSO.copyfolder Source:=Sourcefilename, Destination:=Destinfilename, OverwriteFiles:= False
但是,脚本在找到现有文件时会出错。任何建议都将不胜感激。请尝试下一个代码:
Sub testCopyFolder()
Dim FSO As Object, SourceFold As String, DestinationFold As String
SourceFold = "Source folder path" ' ending in "\"
DestinationFold = "Destination folder path" ' ending in "\"
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(DestinationFold) Then
FSO.CopyFolder SourceFold, DestinationFold
End If
End Sub
您可以以类似的方式继续复制文件。当然,请使用
FSO.FileExists()
Sub testCopyFolder()
Dim FSO As Object, SourceFold As String, DestinationFold As String
SourceFold = "Source folder path" ' ending in "\"
DestinationFold = "Destination folder path" ' ending in "\"
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(DestinationFold) Then
FSO.CopyFolder SourceFold, DestinationFold
End If
End Sub
您可以以类似的方式继续复制文件。当然,使用FSO.FileExists()
…备份文件夹及其子文件夹而不覆盖
- 以下操作将源文件夹备份到目标文件夹,即复制丢失的文件夹和文件
TESTcopyFolder
只是一个如何使用该解决方案的示例
- 它将调用初始化过程,
backupFolder
,必要时将调用backupFolderCopy
和backupFolderRecurse
- 声明
Private SkipPath As String
和三个过程必须复制到同一个(通常是标准的)模块,例如Module1
代码
Option Explicit
Private SkipPath As String
Sub TESTcopyFolder()
Const srcPath As String = "F:\Test\2020\65412587\Test1"
Const dstPath As String = "F:\Test\2020\65412587\Test2"
backupFolder srcPath, dstPath
' Open Destination Path in File Explorer.
'ThisWorkbook.FollowHyperlink dstPath
End Sub
' Initialize
Sub backupFolder( _
ByVal srcPath As String, _
ByVal dstPath As String, _
Optional ByVal backupSubFolders As Boolean = True)
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
With fso
If .FolderExists(srcPath) Then
backupFolderCopy fso, srcPath, dstPath
If backupSubFolders Then
SkipPath = ""
backupFolderRecurse fso, srcPath, dstPath
End If
MsgBox "Backup updated.", vbInformation, "Success"
Else
MsgBox "Source Folder does not exist.", vbCritical, "No Source"
End If
End With
End Sub
' Copy Folders
Private Function backupFolderCopy( _
fso As Object, _
ByVal srcPath As String, _
ByVal dstPath As String) _
As String
With fso
If .FolderExists(dstPath) Then
Dim fsoFile As Object
Dim dstFilePath As String
For Each fsoFile In .GetFolder(srcPath).Files
dstFilePath = .BuildPath(dstPath, fsoFile.Name)
' Or:
'dstFilePath = Replace(fsoFile.Path, srcPath, dstPath)
If Not .FileExists(dstFilePath) Then
.CopyFile fsoFile.Path, dstFilePath
End If
Next fsoFile
'backupFolderCopy = "" ' redundant: it is "" by default.
Else
.CopyFolder srcPath, dstPath
backupFolderCopy = srcPath
End If
End With
End Function
' Copy SubFolders
Private Sub backupFolderRecurse( _
fso As Object, _
ByVal srcPath As String, _
ByVal dstPath As String)
Dim fsoFolder As Object: Set fsoFolder = fso.GetFolder(srcPath)
Dim fsoSubFolder As Object
Dim srcNew As String
Dim dstNew As String
For Each fsoSubFolder In fsoFolder.SubFolders
srcNew = fsoSubFolder.Path
dstNew = fso.BuildPath(dstPath, fsoSubFolder.Name)
' Or:
'dstNew = Replace(srcNew, srcPath, dstPath)
If Len(SkipPath) = 0 Or Left(srcNew, Len(SkipPath)) <> SkipPath Then
SkipPath = backupFolderCopy(fso, srcNew, dstNew)
backupFolderRecurse fso, srcNew, dstNew
End If
Next
End Sub
选项显式
作为字符串的私有SkipPath
子TESTcopyFolder()
Const srcPath As String=“F:\Test\2020\65412587\Test1”
Const dstPath As String=“F:\Test\2020\65412587\Test2”
备份文件夹srcPath,dstPath
'在文件资源管理器中打开目标路径。
'ThisWorkbook.FollowPath
端接头
“初始化
子备份文件夹(_
ByVal srcPath作为字符串_
ByVal dstPath作为字符串_
可选的ByVal backupSubFolders(布尔值=True)
Dim fso As Object:Set fso=CreateObject(“Scripting.FileSystemObject”)
与fso合作
如果.FolderExists(srcPath)那么
备份文件夹复制fso、srcPath、dstPath
如果备份子文件夹,则
SkipPath=“”
备份文件夹递归fso、srcPath、dstPath
如果结束
MsgBox“备份已更新”,vbInformation,“成功”
其他的
MsgBox“源文件夹不存在”,vbCritical,“无源”
如果结束
以
端接头
'复制文件夹
专用函数备份文件夹副本(_
fso作为对象_
ByVal srcPath作为字符串_
ByVal(路径为字符串)_
作为字符串
与fso合作
如果.FolderExists(dstPath)那么
作为对象的Dim fsoFile
将dstFilePath设置为字符串
对于.GetFolder(srcPath).Files中的每个fsoFile
dstFilePath=.BuildPath(dstPath,fsoFile.Name)
“或:
'dstFilePath=Replace(fsoFile.Path、srcPath、dstPath)
如果不存在.FileExists(dstFilePath),则
.CopyFile fsoFile.Path,dstFilePath
如果结束
下一个文件
'backupFolderCopy=“”冗余:默认情况下为“”。
其他的
.CopyFolder srcPath,dstPath
backupFolderCopy=srcPath
如果结束
以
端函数
'复制子文件夹
专用子备份文件夹递归(_
fso作为对象_
ByVal srcPath作为字符串_
ByVal(路径为字符串)
将fsoFolder设置为对象:设置fsoFolder=fso.GetFolder(srcPath)
将fsoSubFolder设置为对象
新的字符串
新的字符串
对于fsoFolder.SubFolders中的每个fsoSubFolder
srcNew=fsoSubFolder.Path
dstNew=fso.BuildPath(dstPath,fsoSubFolder.Name)
“或:
'dstNew=Replace(srcNew、srcPath、dstPath)
如果Len(SkipPath)=0或Left(srcNew,Len(SkipPath))SkipPath,则
SkipPath=backupFolderCopy(fso、srcNew、dstNew)
备份文件夹递归fso、srcNew、dstNew
如果结束
下一个
端接头
备份文件夹及其子文件夹而不覆盖
- 以下操作将源文件夹备份到目标文件夹,即复制丢失的文件夹和文件
TESTcopyFolder
只是一个如何使用该解决方案的示例
- 它将调用初始化过程,
backupFolder
,必要时将调用backupFolderCopy
和backupFolderRecurse
- 声明
Private SkipPath As String
和三个过程必须复制到同一个(通常是标准的)模块,例如Module1
代码
Option Explicit
Private SkipPath As String
Sub TESTcopyFolder()
Const srcPath As String = "F:\Test\2020\65412587\Test1"
Const dstPath As String = "F:\Test\2020\65412587\Test2"
backupFolder srcPath, dstPath
' Open Destination Path in File Explorer.
'ThisWorkbook.FollowHyperlink dstPath
End Sub
' Initialize
Sub backupFolder( _
ByVal srcPath As String, _
ByVal dstPath As String, _
Optional ByVal backupSubFolders As Boolean = True)
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
With fso
If .FolderExists(srcPath) Then
backupFolderCopy fso, srcPath, dstPath
If backupSubFolders Then
SkipPath = ""
backupFolderRecurse fso, srcPath, dstPath
End If
MsgBox "Backup updated.", vbInformation, "Success"
Else
MsgBox "Source Folder does not exist.", vbCritical, "No Source"
End If
End With
End Sub
' Copy Folders
Private Function backupFolderCopy( _
fso As Object, _
ByVal srcPath As String, _
ByVal dstPath As String) _
As String
With fso
If .FolderExists(dstPath) Then
Dim fsoFile As Object
Dim dstFilePath As String
For Each fsoFile In .GetFolder(srcPath).Files
dstFilePath = .BuildPath(dstPath, fsoFile.Name)
' Or:
'dstFilePath = Replace(fsoFile.Path, srcPath, dstPath)
If Not .FileExists(dstFilePath) Then
.CopyFile fsoFile.Path, dstFilePath
End If
Next fsoFile
'backupFolderCopy = "" ' redundant: it is "" by default.
Else
.CopyFolder srcPath, dstPath
backupFolderCopy = srcPath
End If
End With
End Function
' Copy SubFolders
Private Sub backupFolderRecurse( _
fso As Object, _
ByVal srcPath As String, _
ByVal dstPath As String)
Dim fsoFolder As Object: Set fsoFolder = fso.GetFolder(srcPath)
Dim fsoSubFolder As Object
Dim srcNew As String
Dim dstNew As String
For Each fsoSubFolder In fsoFolder.SubFolders
srcNew = fsoSubFolder.Path
dstNew = fso.BuildPath(dstPath, fsoSubFolder.Name)
' Or:
'dstNew = Replace(srcNew, srcPath, dstPath)
If Len(SkipPath) = 0 Or Left(srcNew, Len(SkipPath)) <> SkipPath Then
SkipPath = backupFolderCopy(fso, srcNew, dstNew)
backupFolderRecurse fso, srcNew, dstNew
End If
Next
End Sub
选项显式
作为字符串的私有SkipPath
子TESTcopyFolder()
Const srcPath As String=“F:\Test\2020\65412587\Test1”
Const dstPath As String=“F:\Test\2020\65412587\Test2”
备份文件夹srcPath,dstPath
'在文件资源管理器中打开目标路径。
'ThisWorkbook.FollowPath
端接头
“初始化
子备份文件夹(_
ByVal srcPath作为字符串_
ByVal dstPath作为字符串_
可选的ByVal backupSubFolders(布尔值=True)
Dim fso As Object:Set fso=CreateObject(“Scripting.FileSystemObject”)
与fso合作
如果.FolderExists(srcPath)那么
备份文件夹复制fso、srcPath、dstPath
如果备份子文件夹,则
SkipPath=“”
备份文件夹递归fso、srcPath、dstPath
如果结束
MsgBox“备份已更新”,vbInformation,“成功”
其他的
MsgBox“源文件夹不存在”,vbCritical,“无源”
如果结束
以
端接头
'复制文件夹
专用函数备份文件夹副本(_
fso作为对象_
ByVal srcPath作为字符串_
ByVal(路径为字符串)_
作为字符串
与fso合作
如果.FolderExists(dstPath)那么
作为对象的Dim fsoFile
将dstFilePath设置为字符串
对于.GetFolder(srcPath).Files中的每个fsoFile
dstFilePath=