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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
calvax
Posts: 9
Joined: Fri Dec 30, 2011 6:10 pm

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

Post by calvax » Mon Jan 02, 2012 10:09 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Mon Jan 02, 2012 10:28 pm

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?

calvax
Posts: 9
Joined: Fri Dec 30, 2011 6:10 pm

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

Post by calvax » Mon Jan 02, 2012 11:00 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Mon Jan 02, 2012 11:48 pm

Thanks! I look forward to seeing what you come up with.

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am

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

Post by marksmithhfx » Sat Jan 07, 2012 7:45 pm

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
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Sun Jan 08, 2012 12:12 am

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

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am

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

Post by marksmithhfx » Sun Jan 08, 2012 3:30 am

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
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Post Reply