Populate list box with restricted file extention/types

I’ve created a File Type Set which shows only particular files types for an Open Dialog - works as expected - but now I’d like to do similar with a list box.

This is the code I’m using to populate the listbox with files from a folder:

[code]
Dim myFolder As FolderItem = SpecialFolder.ApplicationData.Child(myAppDataFilesFolder)

If myFolder Is Nil Then
Return
End If

Dim filecount As Integer = myFolder.Count
For i As Integer = 1 To filecount
Dim f As FolderItem = myFolder.Item(i)
If f <> Nil Then
listSavedFiles.AddRow(f.Name)
End If
Next[/code]

This works as expected and displays ALL the files in that folder.

How do I go about only showing or populating the list with files of a certain type/extension?

I did try using the same File Type Set I created for the open dialog window but get errors.

Try something like this:

if f <> nil and f.NameExtensionMBS = filetypes.myextension.extensions then listSavedFiles.AddRow(f.Name) end if

Thanks Beatrix, unfortunately I don’t have the MBS plugins.

I’ll see if I can sort this out some other way.

Cheers.

What about:

if Right(f.Name, 4)=".mov" then
listSavedFiles.AddRow(f.Name)
end if

Perhaps a bit crude, but it seems to work fine:

If f.Name.InStr(".myExtension") > 0 Then listSavedFiles.AddRow(f.Name) End If

The only issue I can see is if “.myExtension” was used somewhere else in a filename of a different filetype, then it would be added to the list.

Not a deal breaker.

Just now saw your post Kuzey. Why didn’t I think of that?
I’ll check it out.

See FolderItem.Type :stuck_out_tongue:

Yep, that’s better Kuzey because it won’t cause the potential issue described above because it IS at the end of the filename.

I guess I should check if this works if the user hides extensions (Windows).

Thanks Norman, my last post was referring to Kuzeys solution.

I did check out FolderItem, But not FolderItem.Type. I’ll have a look.

type literally tells you “i recognize this file as this filetype you defined” :slight_smile:

you can pretty much get away with checking for type <> “” since that will only be set if it matches one of the types you defined

  If myFolder Is Nil Then
    Return
  End If
  
  Dim filecount As Integer = myFolder.Count
  For i As Integer = 1 To filecount
    Dim f As FolderItem = myFolder.Item(i)    
    If f <> Nil and f.type <> "" Then      // <<<<< might need to check for directories but nothing else
      listSavedFiles.AddRow(f.Name)      
    End If
  Next

Ok, thanks for posting that code Norman - it works but I don’t really understand why.

There is nowhere in that code that defines what valid file extension should be added to the list. Is there a default FileTypeSet?

No
Folderitem.Type is ONLY populated IF the file in question matches one of the FileTypes you defined
Otherwise the file is “unrecognized” and type is empty

Handy way to say “is this a file I know about”

Ahhh! Thanks Norman for your patience. I think “the penny has finally dropped” :slight_smile:

I have two FileTypes that have global scope under the main window. The reason why only one FileType showed up in the list is because the other FileType wasn’t represented in the directory. I just tested this by changing one of the file extensions to the other FileType and then they both showed up in the list - as expected.

I ended up going with Kuzeys suggested code with a slight modification.


If f <> Nil AND f.Name.Right(6) = ".final" Then //Only show *.final files in the list
    listSavedFiles.AddRow(f.Name)
End If

Easier for me to understand it that way.