Retrieving File Type

Hello All

I am working on an app an I am populating a ListBox with the information contained in a folder like, name, date modified and size. I would like to also add the type of file, like what you get on Windows.

  • Microsoft Word Document
  • PNG Image

Is there a way to retrieve the information from the file directly?

Thanks and I wish you the best for 2017

Rick

You can get the extension in FolderItem.Name.

Something like

[code]dim ext() as string = split(f.name, “.”)

select case ext(1)

case “docx”
//Word
case “doc”
//Word 97
case “png”
//png picture
end select[/code]

Hello Michel

Does that mean I have to create a case for every possible type of files available on the computer?

I tried to find a list with all possible extensions and there are a few hundreds possible… I could always create a database with the extension and associated application and search through it when I fill the listbox, but it looks like quite a bit of work.

Are you aware of any simpler way?

Thanks again

Rick

bear in mind the “extension” of a filename does NOT necessarily indicate the contents of the file.
For example, and SQLite database could be “test.db”, “test.sqlite”, “test.mystuff”

even using the app that is desiginated to open a file by default won’t really help, as each user may have different preferences

An alternative way is to use the fileTypeSet (at least for your common files o app files)
Then the FolderItem will have the name in the Type property

You might get some info from the AssocQueryString API call.
(There may be a definition in the WFS by Aaron Ballman)

Thank you all

There is something in Windows called ProgID which might do it. Anyone knows how to access it?

Thanks again

Rick

Not only that, but you also can fall into some genius job: “I’ve changed the file type from avi to mp4. Now my application can open that file !”.

Or, like what I’ve done some times ago: I’ve made an error in a batch rename and set jpg instead of gif as extension image files. Now, I check the images inside tag to decide what will be the file extension.

[quote=306752:@Richard Hille]Hello Michel

Does that mean I have to create a case for every possible type of files available on the computer?

I tried to find a list with all possible extensions and there are a few hundreds possible… I could always create a database with the extension and associated application and search through it when I fill the listbox, but it looks like quite a bit of work.

Are you aware of any simpler way?
[/quote]

All depends what you are after. I am sorry, but your question was about Word and png. What I posted is very simply the answer.

It addresses Word, and png. If that is the only types you want, that is all you need. You certainly don’t need to select case every file.

Now, if you want to identify ALL files, that is an entirely different proposition.

If you don’t really know which file types you want to address, it is difficult to help.

Good luck.

You are right, my initial question was not that clear… Sorry about that. The application I am working on will have to display all files available in a folder. I already have that working but I am trying to add a column where the file type would be also indicated. I am looking for something that is pretty much like what Windows will display.

Thanks for any help.

Rick

MBS may have something that does what you are looking for.

@Richard Hille : This might help. It maps a file extension to a MIME type. It is by no means an exhaustive list, but it might help get you going.

If you want to see it in action, it’s included as part of Xojo Web Frame and it’s used in the demo to serve up static files (stylesheets, images, etc).

Public Function MIMETypeGet(Extension As String) as String Select Case Extension Case "ai" Return "application/postscript" Case "bmp" Return "image/bmp" Case "csv" Return "text/plain" Case "doc" Return "application/msword" Case "docx" Return "application/vnd.openxmlformats-officedocument.wordprocessingml.document" Case "eps" Return "application/postscript" Case "fmp12" Return "application/x-filemaker" Case "fmp7" Return "application/x-filemaker" Case "gif" Return "image/gif" Case "jpeg" Return "image/jpeg" Case "jpg" Return "image/jpeg" Case "json" Return "application/json" Case "mer" Return "text/plain" Case "mov" Return "video/quicktime" Case "mp3" Return "audio/mpeg" Case "mp4" Return "video/mp4" Case "ogg" Return "application/ogg" Case "pct" Return "image/x-pict" Case "pdf" Return "application/pdf" Case "png" Return "image/png" Case "pps" Return "application/vnd.ms-powerpoint" Case "ppsx" Return "application/vnd.openxmlformats-officedocument.presentationml.slideshow" Case "ps" Return "application/postscript" Case "psd" Return "application/psd" Case "rtf" Return "application/rtf" Case "sit" Return "application/x-sit" Case "svg" Return "image/svg+xml" Case "tab" Return "text/plain" Case "tif" Return "image/tif" Case "tiff" Return "image/tiff" Case "txt" Return "text/plain" Case "xls" Return "application/vnd.ms-excel" Case "xlsx" Return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Case "zip" Return "application/x-compressed" Case "zipx" Return "application/x-zip-compressed" Else Return "binary/octet-stream" End Select End Function

@ Tim: thanks for the reply. I think I will start with that and see how it goes

Richard

Tims code, does not guarantee that the returned MIME type is appropriate for the CONTENTS of the file, only that it is appropriate for a file whose content matches the extension.

On Windows, the extension IS the file type. So if for some reason the content does not match, tough luck :stuck_out_tongue: