Ray
Ray

Reputation: 192426

How do I get list of all filenames in a directory using VB6?

What is the simplest way in VB6 to loop through all the files in a specified folder directory and get their names?

Upvotes: 8

Views: 90149

Answers (6)

Sajid Dewan
Sajid Dewan

Reputation: 21

'For VB6 very Tricky: 'Simply get the location of all project .frm files saved in your disk/project directory

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

Upvotes: 2

Codemaker2015
Codemaker2015

Reputation: 15705

You can use the following demo code,

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

Upvotes: 0

Janardhan G
Janardhan G

Reputation: 58

create button with name = browseButton create filelistbox with name = List1

double click on button in design

and code should look like this

Private Sub browseButton_Click()

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

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

done now run it

Upvotes: 0

bigsancho
bigsancho

Reputation: 121

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

Upvotes: 12

raven
raven

Reputation: 18145

DJ's solution is simple and effective, just throwing out another one in case you need a little more functionality that the FileSystemObject can provide (requires a reference to the 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

Upvotes: 5

DJ.
DJ.

Reputation: 16257

sFilename = Dir(sFoldername)

Do While sFilename > ""

  debug.print sFilename 
  sFilename = Dir()

Loop

Upvotes: 18

Related Questions