Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Files in a directory 2

Status
Not open for further replies.

Tommyhat

Programmer
Sep 10, 2004
96
CA
How would i go about listing files in a set directory?

im trying to use a file system object but im starting to think this won't do the trick.


---------------------------------------
2b||!2b that == the_question
 
Code:
Dim fso As Scripting.FileSystemObject
Dim lFolder As Scripting.Folder
Dim lFile As Scripting.File

Set fso = New Scripting.FileSystemObject

Set lFolder = fso.GetFolder("C:\")

For Each lFile In lFolder.Files
    Debug.Print lFile.Name
Next

Or, without FSO

Code:
    Dim rslt As String
    
    rslt = Dir("C:\")
    Do
        Debug.Print rslt
        rslt = Dir
    Loop While rslt <> ""
 
Thanks a bunch, bjd4jc!

---------------------------------------
2b||!2b that == the_question
 
Just to be on the safe side, you should check for a string length of 0 before entering the loop. If you don't and there are no files in the directory, the program will error and crash.

Dim rslt As String

rslt = Dir("C:\")
Do While rslt <> ""
Debug.Print rslt
rslt = Dir
Loop

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top