capture all files from directory

  • Thread starter Thread starter abc
  • Start date Start date
A

abc

Is there a way to capture all of the files for a given
directory? I will then write these file names and some
selected attributes of the files to a table.
 
Is there a way to capture all of the files for a given
directory? I will then write these file names and some
selected attributes of the files to a table.

This writes the names of the files to a List Box Row Source.
Adapt it to write to a table.

Public Sub GetFileNames()
' This will fill a list box with all of the file names in a Folder.

On Error GoTo Err_Handler

Dim intX As Integer
Dim MyPath As String, MyName As String
' Display the names in C:\ that represent directories.

MyPath = "c:\FolderName\"
MyName = Dir(MyPath, vbDirectory)

Do While MyName <> "" ' Start the loop.
intX = 1
If MyName = "." Or MyName = ".." Then
Else
List241.RowSource = List241.RowSource & MyName & ","
End If
MyName = Dir ' Get next file.
Loop
List241.RowSource = Left(List241.RowSource, Len(List241.RowSource) -
1)

If intX = 0 And MyName = "" Then MsgBox "Nothing found"
Exit_Sub:
Exit Sub
Err_Handler:
MsgBox "Error # " & Err.Number & vbCrLf & Err.Description
Resume Exit_Sub
End Sub
 
Thanks, that's a good start. Another question
though...what if I have subfolders? I want to be able to
step through all of these folders\subfolders to get at all
of the files in them. Is there a way to do this without
specifically naming all of the folders in the directory?
I'd like to be able to specify a drive and have the code
step through all of the folders and subfolders and capture
attributes of all of the files.
 
You can also get the subdirectories using the Dir()
function and the GetAttr() funtion. Take a look at the
example for the Dir() funtion in the VB help, it gives a
pretty good example of using the Dir() function to loop
through and find all of the subfolders.

-Ted Allen
 
OK, but I want to get at the actual files that are in the
subfolders...I don't need to identify the folders (other
than for the code to recognize that they are folders and
not files). Once I identify a folder(or subfolder) how do
I "open" the folder and get at the files? Thanks!
 
There are probably a few ways that you could approach
it. Generally I would envision using the Dir() function
to build a list of all of the folders with their paths
(you could place them in a string array), and then
looping through that list and using Dir() to return all
of the files from each of the folders (using code similar
to what was provided in the earlier post).

-Ted Allen
 
Back
Top