Find correct filetype of a FolderItem

I have an application (not my application) that has renamed all files to a UUID without extension.
Example:
Original name: MyDocument.pdf
After add to the application: 322373D6-C62C-43A9-A0A3-8854CB118E41

If I try to open these files, someone can open (with FolderItem.open), if it is a text file, but someone can’t opened with message “File xx can’t opened”. Some of these files can’t opened in Finder, because it will be executed in a terminal (it is not a problem with the file permission). if I open a terminal and enter “file 322373D6-C62C-43A9-A0A3-8854CB118E41” ist tells me, that this file is a PDF document. If I rename this file from 322373D6-C62C-43A9-A0A3-8854CB118E41 to 322373D6-C62C-43A9-A0A3-8854CB118E41.pdf the finder can open it.

So I would rename these files and add the filetype extension. The FolderItem.Type doesn’t show a filetype. When I use NSFileManagerMBS I got “NSFileTypeRegular”. I know, I can use a “shell” to start the command line “file”, but my app is build for MAS and I can’t use a shell to cmd “file”.

if f <> Nil then
var fileManager As New NSFileManagerMBS
var error as NSErrorMBS
var Values As Dictionary = fileManager.attributesOfItemAtPath(f, error)
TextField1.Value = f.NativePath
TextField2.Value = f.Type
TextField3.Value = values.Value(fileManager.NSFileType)
end if

Has anyone an idea for solve this problem?

Without an extension there is no foolproof way of determining a file’s type. Having said that, the first couple of bytes of a file often provide clues. For example, PDF files start with the ASCII string ‘%PDF’, JPEG files start with FFh D8h, TIFFs start with 49h 49h 2Ah 00h or 4Dh 4Dh 00h 2Ah. If you know the possible file types you can write an application that determines the file type from the contents and adds the matching extension.

Your best bet would be a recent backup though.

if a terminal “file E*”
E9376C3F-ECC5-4DDD-95BA-21E341580E49: TIFF image data, big-endian
EA9D42DD-34C1-4092-BF8C-F6EBB04E580B: TIFF image data, big-endian
ECF9E814-2547-4017-8FFD-12E2294419C0: TIFF image data, big-endian
ED0CBA0E-EC23-4D42-A6C4-ADBDD8F7C73A: TIFF image data, big-endian
ED29E8D5-1A19-421D-B480-A29A56BE3F3E: TIFF image data, big-endian
EDD05AC7-4A6E-45E5-9FF0-DA9EB9820373: TIFF image data, big-endian
EBB6D869-ED80-4B53-A695-17355C52EF94: HTML document text, ASCII text, with very long lines, with CRLF, LF line terminators
EC5CCC74-8584-4103-8992-1B63FFC39ED7: HTML document text, ASCII text, with CRLF, CR, LF line terminators
ED824F44-2715-445E-8C48-AEC8623EE036: PDF document, version 1.3
EDD435F4-A56D-41F7-88D9-812683A43FB4: SMTP mail text, Little-endian UTF-16 Unicode text
EE64E40A-0803-4F77-8068-9DAD08C3AB65: SMTP mail text, ASCII text
EEB3803D-32B9-4BED-862E-3F38158027ED: SMTP mail text, ISO-8859 text, with very long lines
EEBDD631-FE7E-4B16-BA96-A1520022DBF6: SMTP mail text, ASCII text
EF99E401-EE7F-44E9-8290-A96FC6D49B78: PNG image data, 30 x 30, 8-bit/color RGB, non-interlaced

[quote=477468:@Horst Jehle]if a terminal “file E*”
(…)
EBB6D869-ED80-4B53-A695-17355C52EF94: HTML document text, ASCII text, with very long lines, with CRLF, LF line terminators
(…)
EF99E401-EE7F-44E9-8290-A96FC6D49B78: PNG image data, 30 x 30, 8-bit/color RGB, non-interlaced[/quote]

PNG files start with 89h 50h 4Eh 47h and HTML files have ‘<html‘ somewhere (obviously).

Do you have an MBS license? Christian has a few options included there.

Yes, when I use NSFileManagerMBS I got “NSFileTypeRegular”

I totally missed that. When I saw NSxxxxx, my mind went to declares :S …

I see the same result with FolderItem.FileTypeMBS. I’m digging up info on how the “file” command uses the /etc/magic or /usr/share/file/magic/* info to see if we can build a Xojo-only option (I could use it, as well). I’ll PM you with progress.

mdls might be of use as well

Except that’s still using the Shell and that’s a no-no in MAS apps.