【发布时间】:2008-11-15 00:19:48
【问题描述】:
在VB6中循环指定文件夹目录中的所有文件并获取它们的名称的最简单方法是什么?
【问题讨论】:
在VB6中循环指定文件夹目录中的所有文件并获取它们的名称的最简单方法是什么?
【问题讨论】:
sFilename = Dir(sFoldername)
Do While sFilename > ""
debug.print sFilename
sFilename = Dir()
Loop
【讨论】:
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
【讨论】:
DJ 的solution 简单而有效,如果您需要 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
【讨论】:
'对于 VB6 非常棘手: '只需获取保存在磁盘/项目目录中的所有项目 .frm 文件的位置
将 CountVal 调暗为整数 计数值 = 0 cbo.清除
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
【讨论】:
创建名称为browseButton的按钮 创建名称为 List1 的文件列表框
在设计中双击按钮
代码应该是这样的
Private Sub browseButton_Click()
Dim path As String
path = "C:\My Folder"
List1.path() = path
List1.Pattern = "*.txt"
End Sub
完成现在运行它
【讨论】:
您可以使用以下演示代码,
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
【讨论】: