Computer Hope

Microsoft => Microsoft DOS => Topic started by: MIRKOSOFT on April 23, 2019, 01:49:25 PM

Title: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 23, 2019, 01:49:25 PM
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
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 24, 2019, 11:30:01 AM
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


(https://images2.imgbox.com/cd/2a/OtYmCp8Z_o.jpg)


Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 24, 2019, 06:34:45 PM
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
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: BC_Programmer on April 24, 2019, 08:12:50 PM
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.

Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 25, 2019, 12:49:23 AM
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
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 25, 2019, 07:05:33 AM
I asked first 2 bytes of file no matter of type...

Miro
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: BC_Programmer on April 25, 2019, 08:31:37 AM
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.
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 25, 2019, 09:56:13 AM
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.




Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 25, 2019, 11:30:47 AM
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%

Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 25, 2019, 01:46:27 PM
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
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 25, 2019, 01:54:35 PM
I know that first code was not made for <any> filetype
It was meant to work for any file type.
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 25, 2019, 01:58:52 PM
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
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 25, 2019, 02:15:59 PM
But does it work for what you want?
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 25, 2019, 02:26:36 PM
Yes, works and thank you very much.

Miro
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 25, 2019, 02:28:37 PM
That's good.
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 25, 2019, 06:39:58 PM
Now I tried decimal and it works not...
Where do I mistake?
I only removed remark from decimal line and added remark to hexadecimal line...

Miro
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: MIRKOSOFT on April 25, 2019, 06:47:15 PM
Fixed - decimal works too - only removed Hex prefix with correct brackets.

Miro
Title: Re: How to read first 2 bytes of selected file in DOS or command prompt by BAT
Post by: Salmon Trout on April 26, 2019, 12:49:28 AM
Here's why the decimal output didn't work in the second script...

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