加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB遍历目录文件夹2

发布时间:2020-12-17 08:21:08 所属栏目:百科 来源:网络整理
导读:Private Sub Command1_Click()get_folders List1,"c:windows"End SubPrivate Sub get_folders(ByRef List1 As ListBox,ByVal path As String)'引用了Microsoft Scripting RuntimeDim fso As New Scripting.FileSystemObjectDim fd As Scripting.FolderDim f
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读