【发布时间】:2019-01-06 09:56:44
【问题描述】:
尝试使用 fso 技术从源文件夹 C:\ (V) 复制到目标文件夹 C:(All) 但运行代码给出消息运行时错误 53。找不到文件
我想要实现的是从源文件夹 C:\ V 复制所有 xlsx 文件,其中还包含其他文件扩展名 pdf、csv、txt、word..
所有 xlsx 将被复制到文件夹 C:\ALL,
下面这一行出现运行时错误
****FSO.CopyFile Source:=sourcePath & fileExtn, Destination:=destinationPath****
Sub copy_specific_files_in_folder()
Dim FSO As Object
Dim sourcePath As String
Dim destinationPath As String
Dim fileExtn As String
sourcePath = "c:\V"
destinationPath = "c:\all\"
fileExtn = " * .xlsx"
If Right(sourcePath, 1) <> "\" Then
sourcePath = sourcePath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(sourcePath) = False Then
MsgBox sourcePath & " does not exit"
Exit Sub
End If
If FSO.FolderExists(destinationPath) = False Then
MsgBox destinationPath & " does not exit"
Exit Sub
End If
FSO.CopyFile Source:=sourcePath & fileExtn, Destination:=destinationPath
copy_files_from_subfolders
MsgBox "your files have been copied from subfolders of " & sourcePath & "to" & destinationPath
End Sub
Sub copy_files_from_subfolders()
Dim FSO As Object, fld As Object
Dim fsoFile As Object
Dim fsoFol As Object
sourcePath = "c:\V"
targetpath = "c:\all\"
If Right(sourcePath, 1) <> “ \ ” Then sourcePath = sourcePath & “ \ ”
Set FSO = CreateObject(“scripting.filesystemobject”)
Set fld = FSO.GetFolder(sourcePath)
If FSO.FolderExists(fld) Then
For Each fsoFol In FSO.GetFolder(sourcePath).SubFolders
For Each fsoFile In fsoFol.Files
If Right(fsoFile, 4) = “xlsx” Then
fsoFile.Copy targetpath
End If
Next
Next
End If
End Sub
【问题讨论】: