read all files inside a folder including files in subfolders


how to read all files inside a folder including files in subfolders to list in a listbox

if anyone could help me I appreciate it!

I use the code below but it only recognize the folders inside and not what they contain.

Dim i as Integer
Dim folder as Folderitem
Dim Foldername as string

Foldername = volume(0).name + “:”


for i=1 to folder.count
if (folder.item(i).Directory = False) Then

  ListBox1.AddRow folder.item(i).name

end if


Make a recursive method that will read the items in a folder. Loop through the items in the folder and if the item is a folder then call the method on the folder to loop through all of the items within it.

Just pass the base folder to this method:

Sub ListFiles(theFolder as FolderItem) if theFolder<>nil then if theFolder.Directory then for i as Integer=1 to theFolder.Count if theFolder.Item(i).Directory then ListFiles(theFolder.Item(i)) else listbox1.addrow theFolder.Item(i).name end if next end if end if End Sub

Tom, you are the best!

Thank you for your attention and can be sure that today made a new friend here in Brazil!

Thanks again, Paul Vargas

Hi there Paulo - Tom’s solution is a good one… but note that it will omit the names of directories in the file hierarchy. If you want those in your listbox as well, a subtle adjustment will need to be made.

Also, this will add them to the listbox in the order they are found. If you need them in some other order (alphabetical, date created, etc) then you could dump the theFolder.Item(i) into an array of FolderItem that you could then sort however you like before adding the names to the array.

Just my $0.02.


How does trueitem effect the above code?
Is that for links?

More than likely TrueItem is what should be being used. If there are aliases (or whatever term Windows uses for aliases) TrueItem returns the alias itself. If Item is used the file that the alias points to is returned. Item resolves the alias, TrueItem does not.

This is great and just what I am looking for!

When trying to use this I get a syntax error end sub.

Can you tell me how to make this work and a good place to look up sub routines and how they, or one like this, works in Xojo?

Thank you

Justin - have you checked out the user guide? From within Xojo, click on the “Help” menu, then select “User Guide”, then “1: Fundamentals”. Check out the section on methods.

The error you are getting is because you have copied the “end sub” line into a method… Methods are defined in the IDE by putting the declaration into some special boxes (i.e., the method name, parameter list, etc), and then filling out the body of the method in the code editor.

In other words, if you copy:

Sub ListFiles(theFolder as FolderItem) //Whole bunch of code here... End Sub
into a method, you are actually creating a method that looks like this:

Sub ListFiles(theFolder as FolderItem) Sub ListFiles(theFolder as FolderItem) //Whole bunch of code here... End Sub End Sub

Because the IDE puts the first and last line of the method in for you at compile time.