Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim fil As File
With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing FromPath
.Show
FromPath = .SelectedItems(1) & "\"
End With
With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing ToPath
.Show
ToPath = .SelectedItems(1) & "\"
End With
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FromPath)
For Each objSubFolder In objFolder.SubFolders
**Set fils = fsoC.GetFolder(objSubFolder & "\").Files**
For Each fil In fils
If LCase(Right(fil.Name, 3)) = "zip" Then
MsgBox "this is a zip file "
Else
我在标记的行中收到对象所需错误,我不明白为什么?有人能帮助我吗?
答案1
fsoC
似乎未在您的脚本中声明/实例化。也许您想objFSO
再次使用?
编辑 :
Sub test()
Dim fils As Object
Dim fil As Object
Dim FromPath As String
Dim ToPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing FromPath
.Show
FromPath = .SelectedItems(1) & "\"
End With
With Application.FileDialog(msoFileDialogFolderPicker) 'Choosing ToPath
.Show
ToPath = .SelectedItems(1) & "\"
End With
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FromPath)
For Each objSubFolder In objFolder.SubFolders
Set fils = objFSO.GetFolder(objSubFolder & "\").Files
For Each fil In fils
If LCase(Right(fil.Name, 3)) = "zip" Then
MsgBox "this is a zip file "
Else
MsgBox "NOPE"
End If
Next
Next
End Sub
此代码适用于以下文件夹:
A
-- A.1
---- test.txt
---- test.zip