Detect file type of binary or ascii

Is there a clean way to detect for a file its type, either binary or ascii. More specifically, given an arbitrary file, I want to check it to see if it is an ascii file or a binary file. If the file is binary, I want to check it again to see what possible binary type it is: gzip, excel doc, pdf doc, etc.
What tips can you provide.
Thanks.

You could perhaps use a similar convention to perl:
http://www.perldoc.com/perl5.6/pod/func/X.html
The -T and -B switches work as follows. The first block or so of the file is examined for odd characters such as strange control codes or characters with the high bit set. If too many strange characters (>30%) are found, it's a -B file, otherwise it's a -T file. Also, any file containing null in the first block is considered a binary file. If -T or -B is used on a filehandle, the current stdio buffer is examined rather than the first block. Both -T and -B return true on a null file, or a file at EOF when testing a filehandle. Because you have to read a file to do the -T test, on most occasions you want to use a -f against the file first, as in next unless -f $file && -T $file.

Similar Messages

  • Encoding different file types to binary

    Hi, im writing an application which takes different file types and then encodes them to be sent down a noisy channel, after random effects the data must be decoded back to binary. I have done the algorthims for encoding and decoding binary but i dont know how convert the file types to binary. (files such as jpgs and mp3 and txt)

    ...i know how to do the encoding on binary ...
    ...im not sure how to do this...Maybe you could start by explaining the problem in detail to yourself such that you understand it, then come back and tell us.
    Are you concerned about a difference between a JPG file vs a text file? Text files, in the context you are describing, can be treated as binary just as easily. Use a FileInputStream to read it and you get bytes just like any other file.

  • Detecting file type in vba for word

    I have directories full of files on a Mac.  Most of these files are Doc files, some Txt files and some other types. Unfortunately the files don't have file extensions (not hidden but actually no file extensions).  On a PC in a macro I need to open
    these  doc files (from the Mac) and do some manipulations on the files and then save them.  I have a macro which works as long as all the files are Doc files so if I can detect the file type in my macro I can skip the non-Doc files and process only
    the Doc files.
    Is there a way in a vba for word macro to detect a file type other than by the file extension?  Or if that can't be done can I put error trapping on my Open statement that will catch an attempt to Open a non-Doc type file?
    Thanks
    Harry Spier

    If your code is running on Windows and you are in a position to install software on it, you
    may be able to get reasonably reliable detection using dsofile.dll, which you can get here:
    I really do not know whether it will work with files stored or create on Mac (and in any case, if these are old Mac .doc files, you may find that Windows Word cannot open them anyway.
    You need to
     - register the dll via regsvr32
     - in VBE, use Tools->References... to make a reference to"DSO OLE Document Properties Reader 2.1"
     - use code such as the sample below. 
    Option Explicit
    Function IsWordDoc(FullName As String) As Boolean
    ' FullName is the full path name of the file you want to check
    Dim objDocumentProperties As DSOFile.OleDocumentProperties
    IsWordDoc = False
    Set objDocumentProperties = New DSOFile.OleDocumentProperties
    ' or you can add dsoOptionOnlyOpenOLEFiles to the options and
    ' use error trapping to detect -2147217148
    objDocumentProperties.Open sFileName:=FullName, ReadOnly:=True, Options:=dsoOptionDontAutoCreate
    If objDocumentProperties.IsOleFile Then
    If objDocumentProperties.OleDocumentFormat = "MSWordDoc" Then
    IsWordDoc = True
    End If
    End If
    objDocumentProperties.Close savebeforeclose:=False
    Set objDocumentProperties = Nothing
    End Function
    Sub testIsWordDoc()
    Debug.Print IsWordDoc("c:\a\test.doc")
    Debug.Print IsWordDoc("c:\a\test.xls")
    End Sub
    You may also find that the value of the property I am checking varies and that you need to look at other things, such as 
    objDocumentProperties.SummaryProperties.ApplicationName
    but that value definitely varies according to the version of Word used to create the .doc, so you would need to discover what values it can have in a valid .doc.
    Peter Jamieson

  • Change file type from BINARY DOCUMENT to QUicKTIME VIDEO?

    Hi,
    I'm using Bridge CS4 to help catalogue my Quicktime videos (captured via FCP into a capture scratch file) and for the most part it works well. The problem is it seems to list in the "type" column many of the videos as BINARY DOCUMENTS instead of QUICKTIME VIDEO which means I can't play or scrub through them in the preview window. Instead they appear as a single static frame. They still open up into a playable video though when double clicked.
    Adding or removing the extension .mov seems to have no affect.
    Does anyone have a suggestion as to how to get them back into being recognized as QUICKTIME VIDEO?
    Thanks!

    Ooops, I just found the solution. Right-click on clip and select "Purge Cache For Selection", it changes all clips back to the right type.

  • How to recognize file type (bytearray type)

    Hi,
    what ist the simplest way to detect file type or the type of byte[ ]? I mean, for example, how to detect if a "xyz.pdf" is a "really" PDF-File and not some Jpg with ".pfd"-extension.
    Thanx in advance
    vedran

    but there must be a easy wayIn general there is no requirement that a bunch of bytes belong to
    some unique "type". So there is no way to retrieve that type and, in
    particular, no easy way.
    If you want some pragmatic test (and I'm assuming that pdf was
    just an example - you want something general), you could have a
    look at the nix file command. It is open source and comes with
    a big set of useful files describing file headers. It is open source and
    can be seen, for example, here: ftp://ftp.gw.com/mirrors/pub/unix/file/

  • How to convert an ascii file into dBase .dbf file type

    Does any one out there know how to convert an ascii file(which is generated from PL/SQL script) into a .dbf (dBaseIII) file type? Thanks in advance.

    I haven't worked with dBase for about 20 years, but I seem to recall it having an IMPORT command for that purpose. But maybe I'm wrong...

  • Transfering files in binary or ASCII mode

    I am uploading a shopping cart and some files need to be transferred in ASCII mode and some in binary mode. Can I change this setting anywhere, or doesn DW automatically do this based on file extension, or ???? I can't seem to find much information about this in the forums or on the internet anywhere either.
    I'm using CS4 for windows.
    Thanks!

    Hi
    They are talking rubbish in the example given.
    Basically any html/css/javascript/php/sql-statement and many others are written in ascii, whereas a video/image/audio/exe file are in binary, (as series of 0 or 1 bit) when created, as I said all files are in the end in binary but the software that is used by you, or your server is what translates them to or back-to the required format for us mere mortals.
    Transfer the files as normal, (via ftp) but I would advise checking on any support that you may require in using the software, (your shopping cart) because the distinction being made by the cart suppliers, could point to a lack of understanding by them. Many years ago, (in the days of 'blue screen' bulletin boards, (pre web) the distinction was made because of the required 'headers' that must be sent with the file transfer, but this is not required with such files any more as it is handled by the software.
    Your doctype and associated info is an example of a 'header'.
    PZ

  • File type

    hii
    what is the meaning of file type in ws_upload function module
    for excel files what will be the file type

    Hi
    there r 4 possible values for file type...
    'ASC' : ASCII file
    The file is loaded line by line in the transferred table and this should have one-column and be of the type C. The file size is returned in the parameter FILELENGTH.
    'TRU' : ASCII file + line wrap
    If a line in a file does not fit into a row in a table, then the row is filled completely, and the rest of the line is written to the next row in the table. Words are not split, instead they are put into the next row of the table.
    'BIN' : Binary file
    The file is loaded into the transferred table which should have one-column and be of the type X. The table is filled line by line in this case. In this case, the last line need not necessarily be completely filled. For this reason, the parameter FILELENGTH must be used during further processing. The table may be a maximum of 1022 characters 'wide'!
    'DAT' : ASCII file with tab formatting
    The file is loaded line by line into the transferred table. Tabs in the file mean a change of field.
    The file size is returned in the parameter FILELENGTH.
    For ur excel sheet you  can use ASC.....dats wat i normally use...
    Regards
    Winnie

  • How to read the content of .msg file type with attachments in SAP crm?

    Hi All,
    Need to develop a RFC FM which will take any BP number as input
    Read the attachments in the BP and identify the attachment which is an email( .msg).
    Identify by the mime type and file extension
    Scan through the email to get the attachments of that email read the content of the
    attachment as a binary file and return that binary file with file name, size
    etc.
    I need to read the data (in binary format) attachment of an attachment of .msg file type .
    I have done till step 2 .
    Please help me on step 3.
    Thanks&Regards,
    Pratap Naik

    Can any one help me on this ?
    Thanks in advance.
    Regards,
    Pratap Naik

  • Assigning a file type to a java program

    Greeting,
    I'm sorry if this was asked and answered before but I couldn't find it in my (admittedly cursory) search of this forum:
    I wrote a program to display a binary file (OK. There are plenty of hex browsers, but I wanted it MY WAY). It works fine. I put in a hook to accept a file name in the command line arguments (argv). That works fine. I can't figure out how to make (in Windows) a file type assignment so that files of extension .bin are opened with this java program.
    Can someone give me a hint?

    Why don't you just write a batch file that takes a parameter and starts your viewer?
    You can then associate the extension with that batch file.

  • Setting the file type in a file field

    Hi,
    I wonder if any body can help i am new to web application
    development, I am developing an asp application in dreamweaver 8,
    A few of my pages contain a file field which has a file
    browse button at the end of the field which works fine,
    now i want to set the default file type to *.pdf, Currently
    when i run my app in IE 7, when i click on the file browse
    button, the file browse dialog box appears but the default
    file type is *.* (All files), is it possible to set the default
    file type
    to pdf so that when the file browse dialog is displayed the
    default file type is *.pdf in stead of *.*.
    Thanks
    Fifo

    The OS just watch the extension of the file.Not always. If you create a word doc but name it with
    some other extension, the OS still can determine that
    it is a word doc.On which OS?
    if i take foo.exe and i rename foo.doc so my OSwill
    try to open it as a MS Word Document.Again, garbage in, garbage out.What you mean?
    If I create a MS Word Document for a file .doc that is not a real .doc and it is not able to read, i will just lose cpu time and a lot of memory.
    So it is important to understand if I need really to open MS Word watching what is the real file type
    Either way, you're going to be doing way too much
    work (and wheel-reinventing) if you're going to build
    your own detection scheme.In the SDK there is not any file type checker, and if eventually really exists an OS like you say that is able to read inside the file, i prefer to have my own Java class that do that work in a safe and opensource way.
    However in my first post i've given a site with some java code that do what I need, i just was asking if anyone know if there is any ready-to-work java class that is better for doing this work.

  • How do you make console apps the default application for a file type?

    I have a console movie player, mplayer, that I like a lot. I've compiled it from source on my intel core2 macbook pro and it works fine. There is no gui, but to play movie files you open up a terminal and type the command mplayer /path/to/file. I would like mplayer to play a movie file automatically when I double click on the file, but I can't seem to figure out how to do it. I tried setting it in the file info dialogue, but when I go to the path of the binary I want, it is not selected. How can I set a console app as my default application for my file type?

    there is a GUI mplayer for OS X. why don't you use it instead of the command line one?
    if you insist on using the command line mplayer then the only way to do this is to put a GUI wrapper on it. this can be done with automator or apple script for example. but really, why don't you just use the GUI mplayer?

  • Mailexample.sql how do you determine the file type?

    I'm using the mailexample.sql which explains how to use the demo_mail wrapper package for utl_smtp. There's a procedure for attaching text files and a procedure for attaching binary files. I've got binary files to attach to email successfully.
    How can I determine the file type so that I can use the attach_text procedure when attaching text files rather than the procedure for attaching binary files?
    Sandy

    Watch out! I think there is a bug in that code :-
    bug with PL/SQL Sample - maildemo.sql
    To answer your question - the usual way of determining a file type is to look for 'magic numbers' in the file that indicate its type. For example, Windows exe files often (always?) begin with the two letters 'MZ'. Zip files begin with 'PK'; Rar archives begin with 'Rar!'. Text files can begin with anything, and do not have any characteristic marker, but USASCII7 (which is what the format that routine is designed for - I think), is a characterset that only uses the first 7 bits of the byte. You could read the file, and if you find any bytes > 127, then you know it is not a USASCII7 file. I have seen various programs that 'sample' the first 64 or 256 bytes of a file instead of reading the whole file, and they usually get it right.
    Hope this helps.
    Takmeister

  • How to save as Excel file type?

    There were no Excel file type under the "Save As" function.  Is there anyway to include Excel file type under this list?  Snapshot attached. 
    Solved!
    Go to Solution.
    Attachments:
    Export_file_types.png ‏28 KB

    Hi NiCoder,
    You have a numer of different options to get data from the DIAdem Data Portal to Excel:
    1)  Save to TDM or TDMS file and use the free TDM Excel Importer to load the data into Excel
    2)  Save to CSV file and use Excel's built-in ASCII file import feature to load the data into Excel
    3)  Load the gfsexcel.dll GPI-DLL and use the "File >> Save as..." option to export an *.xls data file
    4)  Use a custom VBScript to automate any of the above 3 options
    5)  Use a custom VBScript to directly send each Data Portal data channel to a column of values in Excel via ActiveX
    Options 1) and 2) are easiest if you want a purely interactive solution.  Option 1) will export all the data values as well as all the properties.  Option 2) will export all the data values and the name of each channel in the Data Portal, but none of the other properties.  If you choose to use option 3), know ahead of time that this feature is limited to 255 columns and will not export any custom properties, just the properties that existed back in DIAdem 8.1.  Option 4) can add in all the custom properties that are missing in option 2).  Option 5) can export all the data and all custom properties, but it is also the slowest option and involves the most programming.
    Let me know what sounds best to you,
    Brad Turpin
    DIAdem Product Support Engineer
    National Instruments

  • How to identify the file type in a safe way

    I need to identify the filetype of a file in my disc.
    I watched over internet and i've found this good site:
    http://filext.com/
    that supplys the "magic bytes" for many kind of files.
    I have also looked over internet for some ready class that identify different file types, and I have found FFident:
    http://schmidt.devlib.org/ffident/index.html
    Do you know any other Java package/class that is able to identify correctly the file types?
    Thank you,.

    The OS just watch the extension of the file.Not always. If you create a word doc but name it with
    some other extension, the OS still can determine that
    it is a word doc.On which OS?
    if i take foo.exe and i rename foo.doc so my OSwill
    try to open it as a MS Word Document.Again, garbage in, garbage out.What you mean?
    If I create a MS Word Document for a file .doc that is not a real .doc and it is not able to read, i will just lose cpu time and a lot of memory.
    So it is important to understand if I need really to open MS Word watching what is the real file type
    Either way, you're going to be doing way too much
    work (and wheel-reinventing) if you're going to build
    your own detection scheme.In the SDK there is not any file type checker, and if eventually really exists an OS like you say that is able to read inside the file, i prefer to have my own Java class that do that work in a safe and opensource way.
    However in my first post i've given a site with some java code that do what I need, i just was asking if anyone know if there is any ready-to-work java class that is better for doing this work.

Maybe you are looking for

  • User Exits or BADI for Tcode ME51N

    Hi All, My requirement is i need to check available quantity at the time of Purchase req creation. If PR quantity is available in stock then it should not allow to create new PR. Is there any user exit or Badi for this requirement. Kindly send me. Re

  • Enhancement of Controlling Datasource

    Hi all,. i have a datasource 0CO_PC_01 from product costing which is pulling data from AUFK AUFK, AFPO, COSS, COSP, COSB, COSL, COKEY, TKA09, TKV09 but its not having a date field (technical completed date of production order) but the field is in AUF

  • Iphoto not working

    Process:         iPhoto [7271] Path:            /Applications/iPhoto.app/Contents/MacOS/iPhoto Identifier:      com.apple.iPhoto Version:         9.5 (902.7) Build Info:      iPhotoProject-902007000000000~2 App Item ID:     408981381 App External ID:

  • Error in connecting to oracle instance

    failed to connect to the oracle instance with an error message ERROR ORA-09925 : Unable to create audit trial file Linux Error: 28: No space left on device Additional Information : 09925 Anybody any suggestions ...

  • Leopard Server SUS "SUCatalogLoaderException" failures

    [Posted to Apple Feedback site...anybody else experiencing this?] Hello-- For about a month now, I've been getting a SUS swupd_sync error on numerous Mac OS X 10.5 Servers that I maintain when trying to download the software update catalogs from Appl