vb 遍历文件夹 与遍历文件

来源:互联网 发布:Hec-ras 数据输入方法 编辑:程序博客网 时间:2024/06/08 01:27

Public Sub Search_wenjian() '遍历文件 获得文件名称

Dim sPath As String
'查找文件夹
sPath = Dir(path, vbDirectory)  '查找第一个文件夹
Do While Len(sPath) '循环到没有文件夹为止
   If Left(sPath, 1) <> "." Then '为了防止重复查找
   If GetAttr(path & "\" & sPath) And vbDirectory Then '如果是文件夹则。。。。。。
      Folder_num = Folder_num + 1

      'Folder(b) = path & sPath & "\" '将目录和文件夹名称组合形成新的目录,并存放到数组中
      Folder_file(Folder_num - 1) = sPath
      'Folder_file_path(Folder_num - 1) = path & sPath & "\"
   End If
   End If
 sPath = Dir '查找下一个文件夹
 DoEvents '让出控制权
Loop

End Sub

Public Sub Search_file_in_folder(folder_name As String)  '遍历指定文件夹下的所有文本文件
Dim Files() As String
Dim a As Integer
FileType = "*.*"
sPath = Dir(folder_name & FileType) '查找第一个文件

Do While Len(sPath) '循环到没有文件为止
   a = a + 1
   ReDim Preserve Files(1 To a)
   Files(a) = path & sPath '将文件目录和文件名组合,并存放到数组中

   sPath = Dir '查找下一个文件
   DoEvents '让出控制权
Loop

End Sub

原创粉丝点击