READ/WRITE binary files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

READ/WRITE binary files

#1 Post by einstein1969 » 26 Jan 2014 16:13

Hi to all,

I have seen several method for read and write a binary file, but there is a method that use only dos commands? (ie, not using external utility/compilated pogram or wsh scripts)

einstein1969

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: READ/WRITE binary files

#2 Post by carlos » 26 Jan 2014 16:19

with debug.exe (not available on 64 bit window system).

For read a file for write.
The method is slow.

Example:

Code: Select all

Echo Hello>hello.txt
bin2deb.cmd hello.txt > dohello.cmd
rem look dohello.cmd



bin2deb.cmd

Code: Select all

@Echo OFF

:Bin2Deb
::Version 1.0.2
::Author: Carlos
::Usage: Bin2Deb file
::Print a debug script for rebuild a binary file
SetLocal EnableExtensions EnableDelayedExpansion
Set "FILE=%~f1"
Set "FILENAME=%~nx1"
Set "size=%~z1"
If Not Defined FILE (
Echo None specified file.&Goto :Eof
) Else If Not Exist "!FILE!" (
Echo The specified file not exist.&Goto :Eof
) Else If Exist "!FILE!\" (
Echo You specified a folder.&Goto :Eof
) Else If !size! Equ 0 (
Echo The specified file is empty.&Goto :Eof
) Else If !size! Gtr 0xFEFF (
Echo The size of file is greater than 65279 bytes.&Goto :Eof)
Set /a "max_offset=256+%size%-1"
Set /a "lrow=%max_offset%-(%max_offset%%%16)"
Set /a "ci=3*(%max_offset%%%16+1)-1"
Call :Dec2Hex %size% size
Call :Dec2Hex %max_offset% max_offset
Pushd "!Temp!"
Set "SRCFILE='_SRC_'.DAT"
Del /A /F %SRCFILE% >Nul 2>&1
Copy "!FILE!" %SRCFILE% >Nul
Echo @ECHO OFF
Echo DEL /A /F FILE.DEB FILE.BIN "!FILENAME!" ^>NUL 2^>^&1
Echo FOR %%%%# IN ^(
Echo "NFILE.BIN"
For /F "skip=1 tokens=2* delims=: " %%A In (
'"(Echo d 100 %max_offset%&Echo q) | debug %SRCFILE%"'
) Do (Set "line=%%B" &Set "line=!line:-= !"
If 0x%%A Neq %lrow% (
Echo "E%%A !line:~0,47!") Else (
Echo "E%%A !line:~0,%ci%!"&Goto :Bin2Deb_))
:Bin2Deb_
Echo "RCX"&Echo "%size%"&Echo "W"&Echo "Q"
Echo ^) DO ECHO %%%%~#^>^>FILE.DEB
ECHO TYPE FILE.DEB ^| DEBUG ^>NUL
ECHO REN FILE.BIN "!FILENAME!"
ECHO DEL FILE.DEB ^>NUL
Del /A /F %SRCFILE% >Nul 2>&1
Popd
Goto :Eof
:Dec2Hex
"%ComSpec%" /d /c Exit /b %~1 >Nul
For /f "eol=0 delims=0 tokens=*" %%# In ("%=ExitCode%"
) Do If Not "%%#"=="" (Set %~2=%%#) Else (Set %~2=0)
Goto :Eof


einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#3 Post by einstein1969 » 26 Jan 2014 16:30

I'm sorry Carlos, I forgot to say without debug/mshta/vb/jscript

For use in 64 bit system too.

Einstein1969

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: READ/WRITE binary files

#4 Post by aGerman » 26 Jan 2014 16:31

The mean problem is that you can't handle the '\0' character (ASCII 0). It's because the CMD is a text based C console program and it basically reads and writes strings. Strings in C are always terminated by a '\0' character. That means this character can never belong to the content of a string.

Regards
aGerman

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#5 Post by einstein1969 » 26 Jan 2014 16:40

aGerman wrote:The mean problem is that you can't handle the '\0' character (ASCII 0). It's because the CMD is a text based C console program and it basically reads and writes strings. Strings in C are always terminated by a '\0' character. That means this character can never belong to the content of a string.

Regards
aGerman


Thanks aGerman, I had already told her this in my thread about converting between bases. But it 's possible to manipulate the information in another format? That is turning while you read or write?

einstein1969

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: READ/WRITE binary files

#6 Post by aGerman » 26 Jan 2014 17:29

I don't understand the meaning of "another format" :?
Basically every file is a binary file because it's content is always written from ones and zeros to the memory. You cannot write these ones and zeros (bits) directly. Only their representation as a character with a width of 8 bits (1 byte) can be written using the CMD or other Windows conole tools. Maybe you can handle each of the other 255 characters but I fear you won't find a workaround for '\0' without a 3rd party.

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: READ/WRITE binary files

#7 Post by dbenham » 26 Jan 2014 17:34

For reading binary files using native batch, see hexDump.bat. It uses an FC hack to read any binary file, including nul bytes - something that jeb taught me in another thread.

For writing any character except nul byte, using only native batch, see charlib.bat

Finally, CERTUTIL can encode any binary file as hex strings, and decode hex strings back into binary. Unfortunately it is not standard on XP. See http://ss64.org/viewtopic.php?pid=6127 for more info.


Dave Benham

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: READ/WRITE binary files

#8 Post by penpen » 26 Jan 2014 17:37

einstein1969 wrote:I have seen several method for read and write a binary file, but there is a method that use only dos commands? (ie, not using external utility/compilated pogram or wsh scripts)
I fear you have to list all commands that are allowed for this task.

For example, you may use fc.exe to read binary files (suboptimal, but possible).
(Edit: dbenham was faster :) )
And if you allow to have a file containing a nullbyte, a file containing '\01', ... , a file containing '\FF', then you can write all bytes to a file using "copy /B ... + /B "(again suboptimal).

But i don't know if this is a solution, that you would accept.

penpen

Edit2: Changed a misleading description.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#9 Post by einstein1969 » 26 Jan 2014 18:00

@aGerman
I intend to format a format other than the standard one. I was just thinking to octal or hexadecimal or decimal 4 bytes used to represent the numbers of dos.
I'm looking for direct or indirect method. The copy command or fsutils or forfiles can handle this?

@dbenham
hexdump looks like a solution for reading!
certutils could be a solution in a few years when xp will no longer be used. But maybe powershell replace dos batch ....

@penpen
the utility that are preinstalled and common at multiple versions of the operating system possible. Copy looks like a solution for writing!


fsutil , forfiles are present in XP and later?

einstein1969

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: READ/WRITE binary files

#10 Post by dbenham » 26 Jan 2014 18:07

penpen wrote:And if you allow to have a file containing a nullbyte, a file containing '\01', ... , a file containing '\FF', then you can write all bytes to a file using "copy /B ... + /B "(again suboptimal).

Ooh, such a simple, slow, and pragmatic solution for writing binary data :D

Really only a single file containing a single null byte is needed. The rest of the characters can be handled normally, as in charlib.bat


Dave Benham

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: READ/WRITE binary files

#11 Post by Aacini » 26 Jan 2014 18:13

dbenham wrote:Really only a single file containing a single null byte is needed. The rest of the characters can be handled normally, as in charlib.bat

Dave Benham

And this is easily created via:

Code: Select all

fsutil file createnew zero.txt 1

Anyway, fsutil is needed for the reading part of binary files...

Antonio

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#12 Post by einstein1969 » 26 Jan 2014 18:15

Aacini wrote:Anyway, fsutil is needed for the reading part of binary files...

Antonio


why? in what way?

einstein1969

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: READ/WRITE binary files

#13 Post by carlos » 26 Jan 2014 18:16

If I remember good: fsutil require for run a administrator account.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#14 Post by einstein1969 » 26 Jan 2014 18:21

dbenham wrote:
penpen wrote:And if you allow to have a file containing a nullbyte, a file containing '\01', ... , a file containing '\FF', then you can write all bytes to a file using "copy /B ... + /B "(again suboptimal).

Ooh, such a simple, slow, and pragmatic solution for writing binary data :D

Really only a single file containing a single null byte is needed. The rest of the characters can be handled normally, as in charlib.bat


Dave Benham

yes, only the zero is a exception, and more files can handle one or more contiguos zero for speedup/caching.

@carlos
thanks for the note. I'll have to think about it for a while...


einstein1969
Last edited by einstein1969 on 26 Jan 2014 18:23, edited 1 time in total.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: READ/WRITE binary files

#15 Post by Aacini » 26 Jan 2014 18:22

einstein1969 wrote:
Aacini wrote:Anyway, fsutil is needed for the reading part of binary files...

Antonio


why? in what way?

einstein1969

The method to read a binary file (that I borrowed from this aGerman's post) consist in create a file with binary zeros of the same size of the original file and then use FC /B command to get the Ascii hexadecimal representation of all bytes different than zero; bytes with zero value are inserted in the "holes" of this representation.

I don't know if a different method to read a binary file in Batch exist that does not use FSUTIL...

Antonio

Post Reply