Get file names from within a folder

  • Thread starter Thread starter thad
  • Start date Start date
T

thad

Hi All,

I am trying to use the FileSystemObject to get the names
of the files with their extensions in a particular
folder. I don't have a problem defining the path to the
folder I want to get the files. I just can't get the
filenames in the folder. A little code or link would be
appreciated.
 
Function ShowFileAccessInfo(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = f.Path & "<br>"
s = s & "Created: " & f.DateCreated & "<br>"
s = s & "Last Accessed: " & f.DateLastAccessed & "<br>"
s = s & "Last Modified: " & f.DateLastModified
ShowFileAccessInfo = s
End Function
 
I am trying to use the FileSystemObject to get the names
of the files with their extensions in a particular
folder. I don't have a problem defining the path to the
folder I want to get the files. I just can't get the
filenames in the folder. A little code or link would be
appreciated.

You don't need to bother with the FileSystemObject to obtain a list of
filenames. Instead, just use the VBA function Dir() to obtain your list. The
following example code will return a list of filenames from the directory path
passed to it (copy the function to a standard module - a global module, not one
behind a form or report):

'*************EXAMPLE START
Public Function fListFilesToMsgBox(strDirectory As String)
' Comments : Displays list of files in specified directory
' in a message box
' Parameters: strDirectory - A string containing a directory path
' Returns : <none>
' Created : 09/11/03 16:58 BMT
' Modified :
' Usage : Call using syntax below
' fListFilesToMsgBox "c:\"
' --------------------------------------------------

On Error GoTo fListFilesToMsgBox_ERR

'Define variables
Dim strList As String
Dim strFileName As String

'Ensure directory exists
If Len(Dir(strDirectory, vbDirectory) & "") = 0 Then
MsgBox "The specified directory does not exist." _
, vbOKOnly + vbInformation, "Invalid Directory"
Exit Function
End If

'Ensure proper format of directory argument
If Right(strDirectory, 1) <> "\" Then
strDirectory = strDirectory & "\"
End If

'***************************
'Get filenames
strFileName = Dir(strDirectory & "*.*", vbNormal)
'If files in the directory, generate list
If Len(strFileName & "") > 0 Then
strList = strFileName
Do
'Get next filename
strFileName = Dir()
If strFileName > "" Then
strList = strList & vbNewLine & strFileName
Else 'No more files
Exit Do
End If
'Clear the variable
strFileName = ""
'Limit list to 50 filenames - number is arbitrary
If UBound(Split(strList, vbNewLine)) > 50 Then
MsgBox "There are too many files to list." _
, vbOKOnly + vbExclamation, "Too Many Files"
Exit Function
End If
Loop
'***************************

'Show list
MsgBox "File List For " & strDirectory & " is:" _
& vbNewLine & vbNewLine & strList _
, vbOKOnly + vbInformation, "File List"
Else 'There are no files in the specified directory
MsgBox "There are no files in the specified directory." _
, vbOKOnly + vbInformation _
, "File List Is Empty"
End If

fListFilesToMsgBox_EXIT:
Exit Function

fListFilesToMsgBox_ERR:
MsgBox "Error " & Err.Number _
& " occurred in fListFilesToMsgBox: " & Err.Description
Resume fListFilesToMsgBox_EXIT

End Function
'*************EXAMPLE END

Now, simply call the function in your debug window using the following syntax:

fListFilesToMsgBox "c:\"
 
Back
Top