我正在尝试使用fso.folder copy在网络驱动器上创建备份数据库。我的意图是移动文件夹中的所有文件,但如果备份驱动器上已存在某个文件,请跳过该文件,然后复制该文件夹中的其余文件。我目前有
SourceFileName="C:\users\desktop\test1"
DestinFileName="C:\users\desktop\test2"
FSO.copyfolder Source:=Sourcefilename, Destination:=Destinfilename, OverwriteFiles:= False但是,该脚本在找到现有文件时会出错。任何建议都将不胜感激。
发布于 2020-12-23 02:05:10
复制文件而不覆盖
,
folder part.上应用On Error
代码
Option Explicit
Sub copyFilesNoOverwrite()
Const srcFolderPath As String = "C:\users\desktop\test1"
Const dstFolderPath As String = "C:\users\desktop\test2"
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(srcFolderPath) Then
MsgBox "Source Folder doesn't exist.", vbCritical, "No Source"
Exit Sub
End If
If .FolderExists(dstFolderPath) Then
Dim Sep As String: Sep = Application.PathSeparator
Dim fsoFile As Object
Dim FilePath As String
For Each fsoFile In .GetFolder(srcFolderPath).Files
FilePath = dstFolderPath & Sep & fsoFile.Name
If Not .FileExists(FilePath) Then
.CopyFile _
Source:=fsoFile.Path, _
Destination:=FilePath
End If
Next fsoFile
Else
.CopyFolder _
Source:=srcFolderPath, _
Destination:=dstFolderPath
End If
End With
End Sub
Sub copyFilesNoOverwriteOnError()
Const srcFolderPath As String = "C:\users\desktop\test1"
Const dstFolderPath As String = "C:\users\desktop\test2"
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(srcFolderPath) Then
MsgBox "Source Folder doesn't exist.", vbCritical, "No Source"
Exit Sub
End If
If .FolderExists(dstFolderPath) Then
Dim Sep As String: Sep = Application.PathSeparator
Dim fsoFile As Object
For Each fsoFile In .GetFolder(srcFolderPath).Files
On Error Resume Next
.CopyFile _
Source:=fsoFile.Path, _
Destination:=dstFolderPath & Sep & fsoFile.Name, _
OverwriteFiles:=False
On Error GoTo 0
Next fsoFile
Else
.CopyFolder _
Source:=srcFolderPath, _
Destination:=dstFolderPath
End If
End With
End Subhttps://stackoverflow.com/questions/65412587
复制相似问题