前端之家收集整理的这篇文章主要介绍了
VB遍历目录文件夹2,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Private Sub Command1_Click()
get_folders List1,"c:\windows"
End Sub
Private Sub get_folders(ByRef List1 As ListBox,ByVal path As String)
'引用了Microsoft Scripting Runtime
Dim fso As New Scripting.FileSystemObject
Dim fd As Scripting.Folder
Dim fd_1 As Scripting.Folder
Set fd = fso.GetFolder(path)
For Each fd_1 In fd.SubFolders
List1.AddItem fd_1.path
Next
End Sub
'程序代码 '遍历文件夹
Private Sub ShowFolderList(folderspec)
Dim fs,f,f1,s,sf
Dim hs,h,h1,hf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set sf = f.SubFolders
For Each f1 In sf
List1.AddItem folderspec & "\" & f1.Name
Call ShowFolderList(folderspec & "\" & f1.Name)
Next
End Sub
'程序代码 '遍历某文件夹下的文件
Private Sub Showfilelist(folderspec)
Dim fs,fc,s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
List1.AddItem f1.Name
Next
End Sub
'程序代码 '遍历某文件夹及子文件夹中的所有文件
Sub sosuofile(MyPath As String)
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i,idir As Long
If Right(MyPath,1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath,vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname
Else
List1.AddItem MyPath & Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
'在这里可以处理目录中的文件
'Fn.Name '得到文件名
'Fn.Size '得到文件大小
'Fn.Path '得到文件路径
'Fn.Type '得到文件类型
'Fn.DateLastModified '得到文件最后的修改日期
End Sub
Private Sub Command1_Click()
ShowFolderList "c:\windows"
End Sub
Private Sub Command2_Click()
Showfilelist "c:\windows"
End Sub