Determine file type, like "file" cmd in unix shell
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Determine file type, like "file" cmd in unix shell
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
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
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?
myFile.myType
how would you expect to figure out what it is?
Re: Determine file type, like "file" cmd in unix shell
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
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
Thanks! I look forward to seeing what you come up with.
-
- VIP Livecode Opensource Backer
- Posts: 931
- Joined: Thu Nov 13, 2008 6:48 am
Re: Determine file type, like "file" cmd in unix shell
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.mwieder wrote:Thanks! I look forward to seeing what you come up with.
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
-- Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS
Targets: Mac, iOS
Re: Determine file type, like "file" cmd in unix shell
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
-
- VIP Livecode Opensource Backer
- Posts: 931
- Joined: Thu Nov 13, 2008 6:48 am
Re: Determine file type, like "file" cmd in unix shell
Brilliant. Thanks Markmwieder 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
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS
Targets: Mac, iOS