Enumerate large number of files to a listbox

I’m running a web app that has a listbox. I need to enumerate all of the files in a folder into the listbox. The way I am doing it works but as the folder has 20,000+ files it takes minutes to do so. Is there a quicker way to list all of the files?

My method is:

dim f as FolderItem

Try
f = GetFolderItem("/Images/")

lvImages.DeleteAllRows

dim u as integer = f.Count

for i as integer = 1 to u
  if not f.item(i).Directory then
    lvImages.AddRow f.item(i).DisplayName
  end
next

catch
msgbox (“Error reading directory”)
end try

Thread and a WebTimer. Have the thread add a few items to a queue each iteration of a loop. Have the WebTimer action add a few items to the WebListBox each time it’s called. The “fews” become parameters, as does the period of the WebTimer.

@Brad Hutchings thanks for the quick reply. That would work for cosmetic purposes but I need to make the entire contents available quickly, not just display them. The customer does a lot of searches and if they have to wait minutes to search that isn’t going to work.

The folderitem documentation says that repeatedly using folderitem.item() can cause a performance hit which would be even more noticeable in a folder with as many files as yours. You may want to try changing your code to the following, although this will likely only provide a small improvement:

[code]dim f as FolderItem

Try
f = GetFolderItem("/Images/")

lvImages.DeleteAllRows

dim u as integer = f.Count

dim tmp as folderitem //a temporary folderitem for use in the loop
for i as integer = 1 to u
tmp = f.item(i) //store f.item(i) in a temporary folderitem to avoid repeated calls to folderitem.item
if not tmp.Directory then
lvImages.AddRow tmp.DisplayName
end
next

catch
msgbox (“Error reading directory”)
end try[/code]

Ok…different approach.

What if I want to have the user enter a search term, i.e. starts with “image”. Is there a way to have GetFolderItem only enumerate those files instead of having to loop through the entire directory listing?