READ/WRITE binary files
Moderator: DosItHelp
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
READ/WRITE binary files
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
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
Re: READ/WRITE binary files
with debug.exe (not available on 64 bit window system).
For read a file for write.
The method is slow.
Example:
bin2deb.cmd
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
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: READ/WRITE binary files
I'm sorry Carlos, I forgot to say without debug/mshta/vb/jscript
For use in 64 bit system too.
Einstein1969
For use in 64 bit system too.
Einstein1969
Re: READ/WRITE binary files
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
Regards
aGerman
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: READ/WRITE binary files
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
Re: READ/WRITE binary files
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
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
Re: READ/WRITE binary files
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
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
Re: READ/WRITE binary files
I fear you have to list all commands that are allowed for this task.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)
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.
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: READ/WRITE binary files
@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
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
Re: READ/WRITE binary files
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
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
Re: READ/WRITE binary files
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
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: READ/WRITE binary files
Aacini wrote:Anyway, fsutil is needed for the reading part of binary files...
Antonio
why? in what way?
einstein1969
Re: READ/WRITE binary files
If I remember good: fsutil require for run a administrator account.
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: READ/WRITE binary files
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
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.
Re: READ/WRITE binary files
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