8

VB6中循环遍历指定文件夹目录中的所有文件并获取它们的名称的最简单方法是什么?

4

6 回答 6

18
sFilename = Dir(sFoldername)

Do While sFilename > ""

  debug.print sFilename 
  sFilename = Dir()

Loop
于 2008-11-15T00:38:43.007 回答
12
Dim fso As New FileSystemObject
Dim fld As Folder
Dim fil As File
Set fld = fso.GetFolder("C:\My Folder")
For Each fil In fld.Files
  Debug.Print fil.Name
Next
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
于 2012-12-20T23:51:40.110 回答
5

DJ 的解决方案简单而有效,如果您需要 FileSystemObject 可以提供的更多功能(需要参考 Microsoft Scripting Runtime),只需放弃另一个解决方案。

Dim fso As New FileSystemObject
Dim fil As File

For Each fil In fso.GetFolder("C:\").Files
  Debug.Print fil.Name
Next
于 2008-11-17T03:23:13.250 回答
2

'对于 VB6 非常棘手:'只需获取保存在磁盘/项目目录中的所有项目 .frm 文件的位置

Dim CountVal As Integer CountVal = 0 cbo.Clear

sFilename = Dir(App.Path & "\Forms\")
Do While sFilename > ""
  If (Right(sFilename, 4) = ".frm") Then
  cbo.List(CountVal) = Left(sFilename, (Len(sFilename) - 4))
  CountVal = CountVal + 1
  End If

   sFilename = Dir()
Loop
于 2018-06-22T03:53:51.187 回答
0

创建名称 = browseButton 的按钮 创建名称 = List1 的文件列表框

双击设计中的按钮

和代码应该是这样的

Private Sub browseButton_Click()

Dim path  As String
path = "C:\My Folder"

List1.path() = path
List1.Pattern = "*.txt"
End Sub

完成现在运行它

于 2015-12-05T11:47:02.763 回答
0

您可以使用以下演示代码,

Dim fso As New FileSystemObject
Dim fld As Folder
Dim file As File
Set fld = fso.GetFolder("C:\vishnu")
For Each file In fld.Files
  msgbox file.Name
Next
于 2018-06-09T02:37:21.830 回答