高手指教,用下面程序搜索扩展名为txt的文件后,For Each tFil In fld.Files出现“拒绝的权限”的错误,为

OptionExplicitDimfsoAsNewFileSystemObjectDimfldAsFolderPrivateSubCommand1_Click()Me.M... Option Explicit

Dim fso As New FileSystemObject
Dim fld As Folder

Private Sub Command1_Click()
Me.MousePointer = vbHourglass
Command1.Enabled = False
Text1.Enabled = False
Text2 = ""
FindFile Dir1.Path, Text1
Text2 = Text2 & vbCrLf & "Search complete"
Text2.SelStart = Len(Text2)
Command1.Enabled = True
Text1.Enabled = True
Me.MousePointer = vbDefault

End Sub
Private Function FindFile(ByVal sFol As String, sFile As String) As Long
Dim tFld As Folder, tFil As File, FileName As String

Set fld = fso.GetFolder(sFol)
For Each tFil In fld.Files
If Mid(tFil.Name, InStrRev(tFil.Name, ".") + 1) = Text1 Then _
Text2 = Text2 & fso.BuildPath(fld.Path, tFil.Name) & vbCrLf
Text2.SelStart = Len(Text2)
DoEvents
Next

If fld.SubFolders.Count > 0 Then
For Each tFld In fld.SubFolders
DoEvents
FindFile = FindFile + FindFile(tFld.Path, sFile)
Next
End If
End Function

Private Sub Drive1_Change()
Dim sDir As String

sDir = Drive1.Drive
If InStr(sDir, "\\") <> 0 Then
sDir = Trim(Mid(sDir, InStr(sDir, "\\")))
If InStr(sDir, "]") <> 0 Then sDir = Left(sDir, InStr(sDir, "]") - 1)
ElseIf InStr(sDir, "[") <> 0 Then
sDir = Trim(Left(sDir, InStr(sDir, "[") - 1))
End If
If Right(sDir, 1) <> "\" Then sDir = sDir & "\"
Dir1.Path = sDir

End Sub

Private Sub Form_Load()
Drive1_Change

End Sub

Private Sub Form_Unload(Cancel As Integer)
End
End Sub
展开
 我来答
徘徊在牛A和牛C间
2011-04-12 · TA获得超过431个赞
知道小有建树答主
回答量:129
采纳率:100%
帮助的人:37.5万
展开全部
由于你访问到了根目录下的System Volume Information目录,该目录的访问是受限的。你可以对fld进行判断,如果包含该目录就跳过。代码如下:
Private Function FindFile(ByVal sFol As String, sFile As String) As Long
Dim tFld As Folder, tFil As File, FileName As String

Set fld = fso.GetFolder(sFol)
If InStr(1, fld, "System Volume Information") = 0 Then
For Each tFil In fld.Files
If Mid(tFil.Name, InStrRev(tFil.Name, ".") + 1) = Text1 Then _
Text2 = Text2 & fso.BuildPath(fld.path, tFil.Name) & vbCrLf
Text2.SelStart = Len(Text2)
DoEvents
Next
If fld.SubFolders.Count > 0 Then
For Each tFld In fld.SubFolders
DoEvents
FindFile = FindFile + FindFile(tFld.path, sFile)
Next
End If
End If
End Function
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式