Page 1 of 1

Determine file type, like "file" cmd in unix shell

Posted: Mon Jan 02, 2012 10:09 pm
by calvax
Hi, and sorry if I write again but I didn't find in dictionary or in documentation.
Is there a function to determine file type, not from extension of it, but from reading the real header and footer of it.
I mean the same function as the command "file" in unix shell.
For example:
In windows a jpg file normally has .jpg or .jpeg or -jpe extension so I could simply parse name and check what is after the "."
But I would like to catch all .jpg files, also if renamed with another extension.
In osx extension are not so significant and it's common to have files without extension.

Does already exist a function that check header or footer of file to determine type, with the same logic of unix command "file"?

Thanks

Re: Determine file type, like "file" cmd in unix shell

Posted: Mon Jan 02, 2012 10:28 pm
by mwieder
There's certainly no cross-platform way to determine a file's type unambiguously. In LiveCode or anywhere else. If there's a particular file type you're interested in you can write your own function to load the binary data and parse it, if you know what you're looking for. But as a generic function, I don't think this is possible. If you come across, for example, a file named

myFile.myType

how would you expect to figure out what it is?

Re: Determine file type, like "file" cmd in unix shell

Posted: Mon Jan 02, 2012 11:00 pm
by calvax
Yes I tought about reading binary data (I am interested to take jpg and png files and discard others) but I hoped that livecode has pre-prepared function to check file types. In such case I will make myself.
If I will make and will work fine I will put online, maybe can be useful for somebody else.
Thanks

Re: Determine file type, like "file" cmd in unix shell

Posted: Mon Jan 02, 2012 11:48 pm
by mwieder
Thanks! I look forward to seeing what you come up with.

Re: Determine file type, like "file" cmd in unix shell

Posted: Sat Jan 07, 2012 7:45 pm
by marksmithhfx
mwieder wrote:Thanks! I look forward to seeing what you come up with.
I did something similar recently when I wanted to check if the file I was reading was SQLite or encrypted, so I loaded the file into a variable and as you say, parsed out the part I was interested in.

Code: Select all

      put URL ("binfile:" & p_pathname) into tData -- copy the file into a variable
      -- check to see if the first word is 'SQLite'
      set itemdelimiter to space
      if item 1 of tData = "SQLite" then
However this copies the whole file which, if large, could create an undesirable delay. Do you know if there is a way to specify only a portion of the file to load into the variable?

-- Mark

Re: Determine file type, like "file" cmd in unix shell

Posted: Sun Jan 08, 2012 12:12 am
by mwieder
Yep - check out the "read from file" command.

Code: Select all

open file tFilePath
read from file tFilePath for 6 -- read the first 6 chars of the file
put it into tText
close file tFilePath
if tText is "SQLite" then
 -- it's a database
end if

Re: Determine file type, like "file" cmd in unix shell

Posted: Sun Jan 08, 2012 3:30 am
by marksmithhfx
mwieder wrote:Yep - check out the "read from file" command.

Code: Select all

open file tFilePath
read from file tFilePath for 6 -- read the first 6 chars of the file
put it into tText
close file tFilePath
if tText is "SQLite" then
 -- it's a database
end if
Brilliant. Thanks Mark