Sign in to follow this  
Followers 0
michaelslamet

Determine file type: text or binary

9 posts in this topic

How to check if a file is a text file or a binary file without see the file extension?

Binary example: doc, swf, exe, zip, rar, jpg, png, gif

Text example: txt, xml, ini, inf, log

Thanks

Share this post


Link to post
Share on other sites



 

Looks promising, especially this library also used in SMF! thanks!

Unfortunetely, no matter what file I put to analyzed, it always display: "Unknown filetype!"

; TrIDlib AutoIt Demo
; http://mark0.net/code-tridlib-e.html

#include "TrIDLib-const.au3"

$MyFile = "c:\temp\11.jpg" ; a random file to analyze
$Buf = "" ; allocate a string for the results

$Dll = DllOpen("TrIDLib.DLL") ; Open TrIDLib DLL

$Ret = DllCall($Dll, "int", "TrID_LoadDefsPack", "str", "") ; load the definitions
$Ret = DllCall($Dll, "int", "TrID_SubmitFileA", "str", $MyFile) ; submit the file
$Ret = DllCall($Dll, "int", "TrID_Analyze") ; perform the analysis

if @error = 1 then
    MsgBox(48, "TrID_Analyze", "Error(s) occurred!")
Else
    $Ret = DllCall($Dll, "int", "TrID_GetInfo", "int", $TRID_GET_RES_NUM, "int", 0, "str", $Buf)
    $ResNum = $Ret[0] ; get the results number
    $Out = ""
    if $ResNum = 0 Then
        $Out = "Unknown filetype!"
    Else
        For $ResId = 1 to $ResNUm ; cycle trough results
            $Ret = DllCall($Dll, "int", "TrID_GetInfo", "int", $TRID_GET_RES_FILETYPE, "int", $ResId, "str", $Buf)
            $Out = $Out & $ResId & ":" & Chr(9) &  $Ret[3] ; get the filetype description
            $Ret = DllCall($Dll, "int", "TrID_GetInfo", "int", $TRID_GET_RES_FILEEXT, "int", $ResId, "str", $Buf)
            $Out = $Out & " (" & $Ret[3] & ")" ; get the filetype extension
            $Ret = DllCall($Dll, "int", "TrID_GetInfo", "int", $TRID_GET_RES_POINTS, "int", $ResId, "str", $Buf)
            $Out = $Out & " - " & $Ret[0] ; get the matching points
            $Out = $Out & Chr(13)
        Next
    EndIf
    MsgBox(0, "TrIDLib demo", $Out)
EndIf

DllClose($Dll) ; cleanup

 

WinXP, AutoIT 3.8.8.1

What did I do wrong?

Share this post


Link to post
Share on other sites

I guess you did not download the TrIDDefs.TRD package from here: http://mark0.net/soft-trid.html

attachicon.gifTrId.zip

 

Thank you again, KaFu. You're correct :)

Now it's working to .jpg and .exe files, but when it come to text file (.txt, .au3) why it always return "Unknown filetype!" ?

Test and see yourself

Share this post


Link to post
Share on other sites

In SMF I replaced "unkown" with "Indeterminable (e.g. text-file) / unknown filetype". There is nothing unique about a txt file, except from the extension it could also be an .au3 file, an .html file or a .py file... TrID is looking for file specific patterns / headers (jpg, zip, exe), but txt files just don't have these kind of patterns.

Share this post


Link to post
Share on other sites

In SMF I replaced "unkown" with "Indeterminable (e.g. text-file) / unknown filetype". There is nothing unique about a txt file, except from the extension it could also be an .au3 file, an .html file or a .py file... TrID is looking for file specific patterns / headers (jpg, zip, exe), but txt files just don't have these kind of patterns.

 

aha! understand now :)

So could I say if it is detected as "unknown", it's quite a big chance it's a text file? Not 100% for sure, but pretty big chance?

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Hmmm, yes, I would think so, not 100% for sure, but pretty big chance is a good summary :)...

 

Ok, I'm going to do some experiment with it. Thanks, KaFu :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0