Traversing a folder for a file type.

If I have a folder I can traverse at like this:

for i as Integer=1 to theFolder.Count if not theFolder.Item(i).Directory then // no recursion necessary dosomething theFolder.Item(i) end if next

But what if I want to switch on the type of file.
i.e. if bmp do this if png do that…
Do I simply look at the last characters of the name?
I had a filetypes object that I use for open dialogs… not sure that’s of any use here…

  for i as Integer=1 to theFolder.Count 
    if not theFolder.Item(i).Directory then
      if right(theFolder.Item(i).Name, 4) = ".bmp" then
        // do this
      elseif right(theFolder.Item(i).Name, 4) = ".png" then
        // do that
      end if
    end if
  next

Folderitem also has a Type property which should match up with the file types in your project.

You may also like:

If InStr(FileType1.All,"png") > 0 Then

and of course, FileType1 have all file types you want to deal with.

Or use Greg suggestion if only one file type to check (or use a Select Case and add as many Cases as needed…)

FolderItem.Count is very expensive in disk io, so to optimize you should get the count only once.

Dim Items As Integer = theFolder.Count for i as Integer=1 to Items

Good catch Wayne: I do not saw it.