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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through Files in a folder 1

Status
Not open for further replies.

ClulessChris

IS-IT--Management
Jan 27, 2003
890
0
0
GB
Can some please help me, I'd like to loop through all files in a folder and add the file names to a list box.

I've been playing with the FileSystemObject but can't seem to fine the right method.

thanks for your help.

Everybody body is somebodys Nutter.
 
dim fso as scripting.filesystemobject
dim fld as folder
dim fle as file

set fso = new systemfilesystemobject
set fld = fso.getfolder("C:\yourfolder")

for each fle in fld.files
listview1.additem fle.filename
next
 
Your a gem, many thanks.

Everybody body is somebodys Nutter.
 
If you want to avoid the bloated (and probably unnecessary) fso then try:

Dim strText As String
strText = Dir("c:\*.txt")
If Len(strText) > 0 Then
List1.AddItem strText
Do
strText = Dir
Debug.Print strText
List1.AddItem strText
Loop While Len(strText) > 0
End If


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Alternatively you could also use a FileListBox.

I don't like the dir function or filelisbox, they are kind of old legacy functions, and don't support network paths etc.

if you wanted to be proper flash you could use the windows api, far less bloated.
 
>>they are kind of old legacy functions, and don't support network paths etc

what do you base this on?

I have no problems using the dir function for paths such as:

Debug.Print Dir("\\server\share\*.*")

Using the API for this simple functionality would be... IMO... silly. Dir works just fine.
 
Vbrit,

on trying your code I get a debug error As the fle.Filename is reconised. what references should I add? (I've already added Microsoft Scripting runtime 1.0)

Everybody body is somebodys Nutter.
 
Many thanks I've now answered my own question. it was fle.name. thanks once again.

Everybody body is somebodys Nutter.
 
Developing from the above, is it possible to list all the files in a folder + all files in the sub folders?
 
Yup. Using recursion.

Have a routine that calls itself for each subfolder found as well as listing all files found in current folder. Without actually coding it, something like:

Sub ListFolder(strFolderName As Folder)
For Each SubFolder In CurrentFolder
Call ListFolder SubFolder
Next SubFolder
For Each File In CurrentFolder
Print File.Path
Next File
End Sub


Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top