更简单的文件遍历方法
0 Reply , Posted in 程序代码 on 2017 28, 2017
Option ExplicitPrivate Sub Command1_Click()
List1.Clear
Dim obj As Object
Dim Fpath As String
For Each obj In Me
If TypeOf obj Is CheckBox Then
If obj.Value = 1 Then
DoEvents
''''不知道为什么 Fpath = CStr(Left(obj.Caption, 1) & ":\") 这里不加斜杠就不能正确遍历C盘
If CStr(Left(obj.Caption, 1)) = "C" Then
Fpath = CStr(Left(obj.Caption, 1) & ":\")
Else
Fpath = CStr(Left(obj.Caption, 1) & ":")
End If
ShowFolderList (Fpath)
End If
End If
Next
End Sub
Sub ShowFolderList(folderspec As String)
On Error Resume Next
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
DoEvents
List1.AddItem folderspec & "\" & f1.Name
Next
Set fc = f.SubFolders
For Each f1 In fc
DoEvents
ShowFolderList (folderspec & "\" & f1.Name)
Next
End Sub