Operating System - HP-UX
1833679 Members
4289 Online
110062 Solutions
New Discussion

Re: determine file type and excel file

 
MR VILLOT   MR MONTAGNE
Frequent Advisor

determine file type and excel file

Hello,

I would like to know how to configure /etc/magic file or anything else to recognize excel format file with file command.

A developper needs to write a script to check format file before importing data in Oracle.
With command file, he has only "data" information.
He uses "strings" command now but I would like to know if there is another solution.

Thanks
Laurent



2 REPLIES 2
Hein van den Heuvel
Honored Contributor

Re: determine file type and excel file

I just looked at an excel file with 'textpad' which goes into data mode for binary files. I see a 'Microsoft Excel' string somewhere in the middle (offset 0x12A0) of my small test file, but I see no easy (binary) pointer to that location early in the file. Nor do I see an 'easy = text' marker in the first bytes.

So I suspect you'll be stuck with strings.
Actually... why not just 'grep'? That would be a little faster. You don't realy want to know what it looks like, just whether it is ter no? I suppose you could have false positives, like someone putting 'how to recognize a Microsoft Excel file' in a text, but you porbably know enough about your business context to evaluate / anticipate that. Perhaps a simple file first to tell you it is just data, not a recognizable other format.
And you are sure you can not simple tell through a file name?

Hein.
Massimo Bianchi
Honored Contributor

Re: determine file type and excel file

Hi,
i would go like this:

every excel file shuld hace an extension .xls -> first check

every excel file should start with thiese exhadecimal values: "320 317 021 340"

You may write a test script like:

#!/sbin/sh
TEST=$(dd if=$1 count=4 bs=1 2>/dev/null| xd -t c | grep "320 317 021 340" | wc -l )

if [ $TEST -gt 0 ]
then
echo "It's excel!!"
fi



Tested on my server, it works..

HTH,
Massimo