Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to read first 2 bytes of selected file in DOS or command prompt by BAT  (Read 10959 times)

0 Members and 1 Guest are viewing this topic.

MIRKOSOFT

    Topic Starter


    Hopeful

    Thanked: 2
    Hi!

    I need to check first 2 bytes of file (no matter of type) by way using it as 1st parameter:

    read_bytes.bat %1

    In command prompt or DOS - best way by simple BAT file...

    Can anybody help?
    Thank you in advance.
    Miro

    Salmon Trout

    • Guest
    Easy in VBscript; hard in batch, so here is a hybrid. You need to uncomment the first red line if you want to see decimal byte values, or the second red line if you want to see hex values

    @echo off
    echo Set fso=CreateObject("Scripting.FileSystemObject") > 2byte.vbs
    echo Set f=fso.OpenTextFile("%1"):buf=f.Read(2):f.Close >> 2byte.vbs
    REM You must UNCOMMENT one (only) of these lines
    REM echo wscript.echo ASCb(mid(buf,1,1)) ^& "," ^& ASCb(mid(buf,2,1)) >> 2byte.vbs
    REM echo wscript.echo Hex(ASCb(mid(buf,1,1))) ^& "," ^& Hex(ASCb(mid(buf,2,1))) >> 2byte.vbs
    for /f "tokens=1,2 delims=," %%A in ('cscript //nologo 2byte.vbs') do (
       set byte1=%%A
       set byte2=%%B
       )
    echo Byte 1 is %byte1%
    echo Byte 2 is %byte2%


    Examples:

    Decimal

    C:\>read_bytes.bat silesian.nzb
    Byte 1 is 60
    Byte 2 is 63


    Hex

    C:\>read_bytes.bat silesian.nzb
    Byte 1 is 3C
    Byte 2 is 3F





    « Last Edit: April 24, 2019, 11:57:21 AM by Salmon Trout »

    MIRKOSOFT

      Topic Starter


      Hopeful

      Thanked: 2
      Works not.

      Really it works only with executables, other files have incorrect values and even using quotes reports error (if were quotes removed it reports what is expected - file not found).

      Miro

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      The Script Salmon provided works for me on seemingly any file. At least where VBScript is available.

      There may be some oddities involved in terms of character encoding for certain files.

      For "Pure" DOS (or the bastardized 7.1 version) If you have Access to QuickBASIC, you can use the following program:

      Code: [Select]
      DIM FFNUM AS INTEGER
      DIM READCHARS AS STRING
      FFNUM = FREEFILE
      OPEN COMMAND$ FOR INPUT AS #FFNUM
      READCHARS = INPUT$(2,FFNUM)
      CLOSE #FFNUM
      PRINT ASC(READCHARS),ASC(MID$(READCHARS,2,1))

      If you build it then you can call the executable with the path of the file it will show the first two bytes. Unless the file is shorter than two bytes, I suppose, in which case it would crash.

      I was trying to dereference Null Pointers before it was cool.

      Salmon Trout

      • Guest
      Script works for me with exe, zip, XML, Excel xlsx, plain and Unicode text files. All checked using hex editor. Will give possibly unexpected results with Unicode encoded text files. You did ask for the first 2 bytes

      MIRKOSOFT

        Topic Starter


        Hopeful

        Thanked: 2
        I asked first 2 bytes of file no matter of type...

        Miro

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        That is what he gave you. He didn't make it specifically work for the types he listed, he tried it with those types to verify it works with "All types".

        You said "other files have incorrect values". How are you verifying the correct values? As Salmon notes, "two bytes" will not always mean "two characters". And the results from some files may not be what you expect but that is because your expectations are wrong, not the results. EXE files for example will tend to always start with "4D 5A" (the MZ header) But a text file that starts with the text "MZ" may start with the bytes FF FE (a Byte Order Marker) or 4D 00 (the UTF-16 representation of M). Those results may be unexpected- but they are not incorrect.
        I was trying to dereference Null Pointers before it was cool.

        Salmon Trout

        • Guest
        1. To deal with quoted filenames, change 3rd line of batch file as in red

        echo Set f=fso.OpenTextFile("%~1"):buf = f.Read(2):f.Close >> 2byte.vbs

        2. OpenTextFile actually opens any file as a binary stream; f.read(n) reads the first n bytes of that stream. These bytes are stored in buf, a string or 1-D array.

        3. The ASCB function....

        Quote
        is used with byte data contained in a string. Instead of returning the character code for the first character, AscB returns the first byte.





        Salmon Trout

        • Guest
        He's right you know... I think results can vary with your current codepage, unless you use MIDB to get the byte out of the string (as well as ASCB). Anyhow, this one uses the ADODB.Stream object.

        Miro, give it a try...

        @echo off
        echo Const adTypeBinary = 1 > 2bytes.vbs
        echo filename = "%~1" >> 2bytes.vbs
        echo Set BinaryStream = CreateObject("ADODB.Stream") >> 2bytes.vbs
        echo BinaryStream.Type = adTypeBinary >> 2bytes.vbs
        echo BinaryStream.Open >> 2bytes.vbs
        echo BinaryStream.LoadFromFile FileName >> 2bytes.vbs
        echo buf = BinaryStream.Read >> 2bytes.vbs
        echo BinaryStream.Close >> 2bytes.vbs
        REM Decimal or...
        rem echo wscript.echo ASCB(MIDB(buf,1,1)) ^& "," ^& ASCB(MIDB(buf,2,1)) >> 2byte.vbs

        REM Hex
        echo wscript.echo Hex(ASCB(MIDB(buf,1,1))) ^& "," ^& Hex(ASCB(MIDB(buf,2,1))) >> 2bytes.vbs
        for /f "tokens=1,2 delims=," %%A in ('cscript //nologo 2bytes.vbs') do (
           set byte1=%%A
           set byte2=%%B
           )
        echo Byte 1 is %byte1%
        echo Byte 2 is %byte2%


        MIRKOSOFT

          Topic Starter


          Hopeful

          Thanked: 2
          That works perfectly!

          To all mentioned things:
          I know that first code was not made for <any> filetype and I know that files have headers and IDs, in case encoding also each character can be written differently.
          When I asked no matter of type I mean always first two bytes - not characters, not other units, first two bytes of file beginning.
          I was very surprised in first code that it was handled as text input, but I don't know VBScript, so I could change only few things.
          Binary access is required for raw data and so is handled last version.
          Quotes were problem and I see in new code that were handled by simple command line parameter.

          Thank you for all your work.
          Miro

          Salmon Trout

          • Guest
          I know that first code was not made for <any> filetype
          It was meant to work for any file type.

          MIRKOSOFT

            Topic Starter


            Hopeful

            Thanked: 2
            I'm sorry for bad English.

            I meant: It was not for <this> filetype.

            I gave into brackets any 'cause it was not for TXT, not for BMP, not for XLS, that it was for each.

            I had to write: It was for each filetype - but I'm not sure if it is in English correct.

            Miro

            Salmon Trout

            • Guest
            But does it work for what you want?

            MIRKOSOFT

              Topic Starter


              Hopeful

              Thanked: 2
              Yes, works and thank you very much.

              Miro

              Salmon Trout

              • Guest
              That's good.