File names in a folder

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

Thanks for taking the time to read my question.

I would like to create a loop that returns all the file names in a folder. I
did this a long time ago, but I can't remember how, and I can't find the db
that I did it in. I know Access has this capability in it, but I can't find
it in help... I'm not using the right terms I guess.

Does anyone know how to do this?

Thanks,

Brad
 
I finally found one that works and is quite simple

Dim FileScript, FolderPath, FileSet, FileSelection


Set FileScript = CreateObject("Scripting.FileSystemObject")
Set FolderPath = FileScript.GetFolder("C:\") 'Path here
Set FileSet = FolderPath.Files


For Each FileSelection In FileSet

Debug.Print FileSelection.Name

Next


End Sub
 
Brad said:
I finally found one that works and is quite simple

Dim FileScript, FolderPath, FileSet, FileSelection


Set FileScript = CreateObject("Scripting.FileSystemObject")
Set FolderPath = FileScript.GetFolder("C:\") 'Path here
Set FileSet = FolderPath.Files


For Each FileSelection In FileSet

Debug.Print FileSelection.Name

Next


End Sub


Yes, that will work, but I don't see the point to incurring the overhead of
creating an FSO when the functionality is built into VBA with the Dir()
function.
 
Brad said:
I finally found one that works and is quite simple

Dim FileScript, FolderPath, FileSet, FileSelection


Set FileScript = CreateObject("Scripting.FileSystemObject")
Set FolderPath = FileScript.GetFolder("C:\") 'Path here
Set FileSet = FolderPath.Files


For Each FileSelection In FileSet

Debug.Print FileSelection.Name

Next


End Sub


Yes, that will work, but I don't see the point to incurring the overhead of
creating an FSO when the functionality is built into VBA with the Dir()
function.
 
(re-sending, as my original reply hasn't appeared)

Brad said:
Thanks for taking the time to read my question.

I would like to create a loop that returns all the file names in a folder.
I
did this a long time ago, but I can't remember how, and I can't find the
db
that I did it in. I know Access has this capability in it, but I can't
find
it in help... I'm not using the right terms I guess.

Does anyone know how to do this?


The basic loop goes like this:

'----- start of example code -----
Dim strFolderPath As String
Dim strFileName As String

strFolderPath = "C:\Your\Path\To\YourFolder"

strFileName = Dir(strFolderPath & "\*.*")

Do Until Len(strFileName) = 0

' ... do something with strFileName

strFileName = Dir()

Loop
'----- end of example code -----

Each time through the loop, strFileName has the name of another file in the
folder. The question then is, what do you want to do with it?
 
Back
Top