Jump to content

how detect file type ?


Recommended Posts

hi guys i want detect if  file  is exe or jpg or other , without use extention , only with analize inside of them , how is possible to do?? i look this topic 

and i downloaded example and run it but not  work :( , anyone have alternative solution ?

 

Link to comment
Share on other sites

Not a complete solution but got the approach from this site: https://blog.systoolsgroup.com/detect-file-type-without-extension/

The file information does appear in the hex string in the two I tested (PDF and RTF).  That being said, probably better to use a third party software that does the heavy lifting for you,

File Signature

Identifying Files that don’t have an Extension

If a file does not have an extension, it still has a file signature. You can identify the format of that file from its file signature. All file types have a standard and unique signature and this data is stored in the file itself. There are programs that can read this signature and determine the file format even if the extension is missing from the file name. All you have to do is get a freeware hex editor that can represent the hexadecimal structure of the file. The first set of bytes in a file represents a signature, which, in most cases, is 2-4 bytes long. If you are capable enough to identify it on your own well and good, otherwise, there is always Google one click away.

 

#include <String.au3>
$aFile=FileOpen(@DesktopDir&"\test",0)
if @error then MsgBox(0,'ERROR',@error)
local $firstLine=filereadline($aFile)
$hexData=_HexToString ( $firstLine )
ConsoleWrite(@crlf&$hexData)

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

with this example  work, but not  compleatly

#include <String.au3>
$aFile=FileOpen(@DesktopDir&"\test",0)
if @error then MsgBox(0,'ERROR',@error)
local $firstLine=filereadline($aFile)
$hexData=_HexToString ( $firstLine )
ConsoleWrite(@crlf&$hexData)

i have 2 excel  one   have inside this

0xPK     ! ���w�  �   [Content_Types].xml �(�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 �U[K�0~�%��f� "�|���brֆ�I�9�ۿ���!�Ł�����.'�/��e�$���bXD^c}U�ח��RdH�傇R� ����d����W{,EM��D]C��<�LCj�g�dTz�*����B��  <��b����j�(�[�ﵒ7�Ev���R�B��V�B�›$y�N��a�ce���1YfL�@��Pȭ�  �#�tUpe'k��`hWv���{��H�@��=���˥��!��B��A���kQ�(�t���6��^�#i�u�=u�����?�A<s ���9p H+xd�k�C̵J`����:����th��M�#s�&lp��s�=��S4A_��V瑁 ��Mhm�

and i can use stringinstring [content_types].xml  or  0xpk

but in second excel i have this o_0

0x?? !??? ? ?????????????�                                                                                                                                                                                                                                                               ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????? ???? ?????????????                                                                                                                                                                                                                                                                ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????? ???????????????????????�                                                                                                                               ???????????????????????????????????????G??????????????????????????????????????????????????z???????????????????????????????????????????????? ??? !??s ?  ????????????????????????????????????????????????????????????????????????t??????????????????????????????????????????????????????????????????????????????????h???????????????????????? ??? !??? ?  ????????????????????????????????????a????????????????????L???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????h?????????????????????????????????????????v???????????????+??????? ??? !??� l # ??????????????????????????????????????????????"???????????????????????????????????????????????????????????????? ??? !??� ?  ????????????????????????????????????????????????????????????7?????????????????????????????????????????g????????????????????????????g????????????????????????????????????????????????????? ??? !??� ?  ????????????????????????????????????????????????????????????7?????????????????????????????????????????g????????????????????????????g????????????????????????????????????????????????????? ??? !??? ?  ???????????????????????????????????????????+????????????????????????????????????????????????????????????????????????????????????????????????????????????????S??????????????????????????????????????????????????????n?????????????+???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????o????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????s???????????????????????????,??????????????????????=???????????????????????????????????????????????t?????????????????????????x????????????????????????????????????????????????????????????????????????????????????????????????????????????????????t???????????????????????????????????????????????????????????????????????????� ??? !??? ? 0x?? !??? ? ??????????????                                                                                                                                                                                                                                                               ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????? ???? ?????????????                                                                                                                                                                                                                                                                ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????? ????????????????????????                                                                                                                               ???????????????????????????????????????G??????????????????????????????????????????????????z???????????????????????????????????????????????? ??? !??s ?  ????????????????????????????????????????????????????????????????????????t??????????????????????????????????????????????????????????????????????????????????h???????????????????????? ??? !??? ?  ????????????????????????????????????a????????????????????L?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????h?????????????????????????????????????????v???????????????+??????? ??? !??? l # ??????????????????????????????????????????????"???????????????????????????????????????????????????????????????? ??? !??? ?  ????????????????????????????????????????????????????????????7?????????????????????????????????????????g????????????????????????????g????????????????????????????????????????????????????? ??? !??? ?  ????????????????????????????????????????????????????????????7?????????????????????????????????????????g????????????????????????????g????????????????????????????????????????????????????? ??? !??? ?  ???????????????????????????????????????????+????????????????????????????????????????????????????????????????????????????????????????????????????????????????S??????????????????????????????????????????????????????n?????????????+???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????o????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????s???????????????????????????,??????????????????????=???????????????????????????????????????????????t?????????????????????????x????????????????????????????????????????????????????????????????????????????????????????????????????????????????????t???????????????????????????????????????????????????????????????????????????? ??? !??? ?

why ? :(

Link to comment
Share on other sites

The first file is an xlsx file, you can tell by the 0xPK, the second one is an xls file regardless of what the extension says it is.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

2 minutes ago, BrewManNH said:

the second one is an xls file regardless of what the extension says it is.

@BrewManNH just for my own benefit ... how can you tell from looking at that?  I am just curious.  I learned a lot looking into this issue but that second file doesn't seem to include a file signature, or at least one that matches the format.  I am sure you are right, but just wanting to understand.

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

If it were an xlsx file, the first 4 characters would be 0xPK, which by the way is the same signature as a zip archive. The reason for this is actually pretty straightforward. The xlsx file format IS a zip file, if you change the extension of it from xlsx to zip, you can open it as if it were an archive. All of the relevant file information for the spreadsheet is inside the archive as individual files.

BTW, this would also confuse any type of file type detector because as far as the signature is concerned, it's still just a ZIP file.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

well, as i suggested earlier, post the hex value of the first bytes.  You will see that you have both values.  Then check the site I provided you. Why you try to make it so complicated ?

Link to comment
Share on other sites

9 minutes ago, faustf said:

is  both  xls 

The first one is almost certainly an xlsx. Trying to open it in Excel you get a warning that the extension doesn't match the file type, changing it to XLSX removes the warning. Also, changing its extension to ZIP and you can open it just like a zip archive,

The second file is the same, an XLSX file with the wrong extension.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

the file xcel is generated  by  another program

it  have  classical routine excel  and  at end  save  with 

Func _EXCEL_CREA()
    ConsoleWrite('@@ (693) :(' & @MIN & ':' & @SEC & ') _EXCEL_CREA()' & @CR) ;### Function Trace

    Local $oAppl = _Excel_Open(True, False, True, True, True)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

    $oWorkbook = _Excel_BookNew($oAppl)

    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "VIA", "A1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "MQ", "B1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "PREZZO", "C1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "PREZZO al MQ", "D1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "%SCOSTAMENTO", "E1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "LINK", "F1")
    $oWorkbook.ActiveSheet.Range("A1:F1").Interior.ColorIndex = 56
    $oWorkbook.ActiveSheet.Range("A1:F1").Font.Bold = True
    $oWorkbook.ActiveSheet.Range("A1:F1").Font.ColorIndex = 2
    ;_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Disponibilita", "G1")
    ;_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Arrivi", "H1")
    ;_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Listino", "I1")
    ;_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Margine", "J1")
    ;_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Prezzo c&c", "K1")
    ;_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Prezzo Web", "L1")
    #cs
          Local $oRange = $oWorkbook.activesheet.Range("A2:F2000")
    With $oRange
        .FormatConditions.Delete ;Delete Existing FormatConditions
        .FormatConditions.Add($xlCellValue1, $xlBetween1, "1", "10") ;FormatConditions(1)
        .FormatConditions.Add($xlCellValue1, $xlBetween1, "0", "-10") ;FormatConditions(2)
    EndWith

    With $oRange.FormatConditions(1)
        .Interior.ColorIndex = 3 ;Red
    EndWith

    With $orange.FormatConditions(2)
        .Interior.ColorIndex = 10 ;Green
    EndWith
    #ce
    Return $oWorkbook
EndFunc   ;==>_EXCEL_CREA

 

do somthing ...

 

_Excel_BookSaveAs($oLExcel, @DesktopDir & "\Risultati_estrazioni\" & $sGNomeFile & ".xls")

 

therfore  i suppose is  all .xls

 

Link to comment
Share on other sites

I am not sure why you need to test a file for filetype that you are creating with the Excel UDF?  That being said, the third parameter in _excel_bookSaveAs, which you are not using, can change the file type.  The default will produce either xls or xlsx depending on the version of Excel (according to the comments in the ExcelConstants.au3) :  .xls for < Excel 2007, .xlsx for > Excel 2007.  I think adding xls to the end of the filename in the path would likely not impact the file type if you have an Excel version > 2007 (just the file name).  

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

because i want  able only send by mail (by another program ) only file xls and txt (txt work) because if  people rename exe  to xls  (for example) the program must not send  nothing , but send me (admin) the allert of some user  want do inappropiate operation (this is a reason)

version of: office 2010

but iformat , i simply use  ??

_Excel_BookSaveAs($oLExcel, @DesktopDir & "\Risultati_estrazioni\" & $sGNomeFile & ".xls","xls")

thankz

 

Edited by faustf
Link to comment
Share on other sites

You should be trying to check if it's an executable, and not worry about whatever else it might be then.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...