VBA 文件复制,并判断文件是否存在,存在则报错!
Function 复制文件(ByVal 旧文件路径 As String, ByVal 旧文件名 As String, ByVal 新文件路径 As String, ByVal 新文件名 As String)
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
If fso.Folderexists(旧文件路径) And fso.Fileexists(旧文件路径 & 旧文件名) And fso.Folderexists(新文件路径) And (Not (fso.Fileexists(新文件路径 & 新文件名))) Then
fso.Copyfile 旧文件路径 & 旧文件名, 新文件路径 & 新文件名, False
Else
MsgBox "出错啦!请检查一下项目:" & vbCrLf _
& "1.查看文件是否存在" & vbCrLf _
& "2.查看路径是否存在" & vbCrLf _
& "3.查看目标目录是否存在该文件" & vbCrLf _
, vbInformation, "VB小源码提醒您!"
End If
End Function
使用方法:
Sub a()
Call 复制文件("C:\Users\sss\Desktop\test1\", "a.txt", "C:\Users\ss\Desktop\test2\", "b.txt")
End Sub