Determing internal format of file

R

RD

We're picking up files on an FTP server using a vb.net program. We need to
know if the file we pick up is an ASCII comma separated file or if it is in
an Exel format without relying on the extension of the file name
exclusively. So we have to look in the file itself using Vb .Net program
code.

By looking at the contents of the file using a function or sub, how can we
determine reliably if it is an ASCII file or an Excel file?

Thanks for any help,

Bob
 
H

Hal Rosser

read 2 or 3 lines - count the commas in each line - if all 3 lines contain
the same number of commas,
i bet its a comma-delimited file - or you could check for chars unique to
excel files (you have to look to see)
 
O

Oenone

By looking at the contents of the file using a function or sub, how
can we determine reliably if it is an ASCII file or an Excel file?

Prior to Excel 2003 (which changed the format of .xls files), Excel files
were binary files. If you open one in a binary editor (or a good text editor
such as Ultra Edit) you'll find that the first 100 or so bytes primarily
consist of either 0x00 of 0xff characters (there are others too but there
are lots of these). These characters will never appear in a .csv file unless
it has been corrupted. So you could just open it, scan the first 64 bytes or
so and see if any contain 0x00 or 0xff. If they do it's an Excel file. If
they don't, it's not (though that doesn't necessarily mean that is is a .csv
file, of course).

I've no idea about the format in which Excel 2003 files are stored, but
wouldn't be surprised if they're XML-based (in which case they would be
ASCII files, you'll need to find this out).

ASCII files should only ever contain characters in the range 32-127
(inclusive), as well as ascii 10 and 13 (line feed and carriage return).

Hope that's of some help,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top