VB编程:获取文件夹下所有文件

简介: VB编程:获取文件夹下所有文件

1.jpg

Private Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String) As Long

   DoEvents

   Static Files As Long

   Dim sDir As String

   Dim sSubDirs() As String

   Dim Index As Long

   Dim sFiles() As String

 

   If Right(sPath, 1) <> "\" Then sPath = sPath & "\"

   '获取文件名和数目

 

   sDir = Dir(sPath & sFileSpec)

 

   Do While Len(sDir)

 

      Files = Files + 1

      ReDim Preserve sFiles(1 To Files)

      sFiles(Files) = sPath & sDir

   

      '显示到列表

      List1.AddItem sFiles(Files)

      List1.ListIndex = List1.ListCount - 1

   

      sDir = Dir

   Loop

   '获取文件夹名称

   Index = 0

   sDir = Dir(sPath, vbDirectory)

   Do While Len(sDir) 'sDir <> ""

       If sDir <> "." And sDir <> ".." Then

           If GetAttr(sPath & sDir) And vbDirectory Then

               Index = Index + 1

               ReDim Preserve sSubDirs(1 To Index)

               sSubDirs(Index) = sPath & sDir & "\"

           End If

       End If

       sDir = Dir

   Loop

   '递归调用,获取子文件夹目录

   For Index = 1 To Index

       Call TreeSearch(sSubDirs(Index), sFileSpec)

   Next Index

   TreeSearch = Files

End Function



Private Sub Command1_Click()

 

   Print TreeSearch(Text1.Text, Combo1.Text)

 

End Sub



Private Sub Command2_Click()

   End

End Sub



'Private Sub Command3_Click()

'    List1.Clear

'End Sub



Private Sub Form_Load()

   Text1.Text = "D:\vb小程序\vb实例"

   Dim spec As Variant

   spec = Array("*.*", "*.exe", "*.vbp")

   Combo1.Text = spec(0)

   For i = 0 To UBound(spec)

       Combo1.AddItem spec(i)

   Next i

End Sub


相关文章
|
4月前
|
C++
MFC编程 -- 记事本项目(大体框架)
MFC编程 -- 记事本项目(大体框架)
39 0
|
1月前
|
编译器 C# Windows
C#基础:手动编译一个.cs源代码文件并生成.exe可执行文件
通过上述步骤,应该能够高效准确地编译C#源代码并生成相应的可执行文件。此外,这一过程强调了对命令行编译器的理解,这在调试和自动化编译流程中是非常重要的。
98 2
VB中项目工程保存基础知识
VB中项目工程保存基础知识
VB中的文件夹和文件路径的控制【VB学习笔记2020课堂版11】
VB中的文件夹和文件路径的控制【VB学习笔记2020课堂版11】
271 0
VB中的文件夹和文件路径的控制【VB学习笔记2020课堂版11】
vb写的一个小解释器(暂定命名s++) 功能还很弱很弱
vb写的一个小解释器(暂定命名s++) 功能还很弱很弱
90 0
|
小程序
VB编程:获取文件夹下所有文件-1
VB编程:获取文件夹下所有文件-1
362 0
VB编程:获取文件夹下所有文件-1
|
Shell
VB编程:用Shell函数打开记事本-54
VB编程:用Shell函数打开记事本-54
|
Shell
VB编程:用Shell函数打开记事本
VB编程:用Shell函数打开记事本
358 0
VB编程:用Shell函数打开记事本
VB编程:FileLen函数获取文件的大小
VB编程:FileLen函数获取文件的大小
299 0
|
索引 Windows
CHM帮助文件在VB程序中的应用
CHM帮助文件在VB程序中的应用
858 0