How to get a list of files id directory (analog of DOS command Dir)?


P

Panda

How to get a list of files id directory (analog of DOS command Dir)?
 
Ad

Advertisements

N

Nikos Yannacopoulos

Depends on what you want to do with it. Here's some sample code that
scans all files in a specific folder, and puts the names of all Excel
sheets in a listbox on a form:

Private Sub Form_Open(Cancel As Integer)
Dim fldr, fls, fl
Dim db As DAO.Database
Dim rst As DAO.Recordset
CurrentDb.Execute "DELETE * FROM tblFiles", dbFailOnError
Set fs = CreateObject("Scripting.FileSystemObject")
Set fldr = fs.GetFolder("C:\documents\access\")
Set fls = fldr.Files
Set db = CurrentDb()
Set rst = db.OpenRecordset("tblFiles")
On Error Resume Next
For Each fl In fls
If Right(fl.Name, 4) = ".xls" Then
rst.AddNew
rst.Fields(0) = fl.Name
rst.Update
End If
Next fl
On Error GoTo 0
rst.Close
Set rst = Nothing
Set db = Nothing

Me.List0.RowSource = "SELECT FileName FROM tblFiles"
Me.List0.Requery
End Sub

As you see, the code is in the form's On Open event, and puts the file
names in a table called tblFiles, with a single text field, where from
they are pulled into the listbox (named List0 in the example).

HTH,
Nikos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top