Page 1 of 1
Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 20 Mar 2012 15:13
by miskox
Hi all,
is it possible (I am aware of some constraints from other topics regarding some special characters) to read a file (any type:exe, doc, pdf, txt...) byte by byte and write a dec (or hex) value of each byte? Something like dumping a file. Of course standard (part of Windows) programs can be used if they help (find, findstr....). No third party software. Preferably for XP of course.
Thanks,
Saso
P.S. I needed a program to do this but I had to write one myself at the end in COBOL. Unfortunately I only have a 16-bit DOS version of MS COBOL (developed by Micro Focus) I still use (in a virtual DOS machine). But it is still usable. Standalone exe has less than 40k bytes!
Re: Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 20 Mar 2012 17:15
by foxidrive
Windows Scripting Host and VBS can probably be used to do that.
Re: Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 20 Mar 2012 17:19
by foxidrive
I had this squirreled away from a Usenet post by Todd Vargo.
this solution is a vbscript for Win98+ systems.
'hexdump.vbs
'Usage:
'cscript /nologo hexdump.vbs <infile.ext >hexdump.txt
'cscript /nologo hexdump.vbs -r <hexdump.txt >outfile.ext
'
Dim StdIn, StdOut
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
If WScript.Arguments.Count = 0 Then
Do While Not StdIn.AtEndOfStream
StdOut.Write Right("0"+Hex(Asc(StdIn.Read(1))),2)
n = n + 1
If n = 35 Then
n = 0
StdOut.Write vbCrLf
End If
Loop
ElseIf WScript.Arguments.count = 1 Then
If Lcase(WScript.Arguments(0)) = "-r" Then
Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine
For i = 1 to Len(str) step 2
StdOut.Write Chr("&H"+Mid(str,i,2))
Next
Loop
End If
End If
Now, you can either use it on command line using the syntax as shown at
the top of the script, or create a batch wrapper to invoke it. This is a
simple batch example without any error trapping. I leave it for you to
improve as deemed necessary.
::hexdump.bat
@echo off
if %1.==-r. goto :reverse
cscript /nologo hexdump.vbs <%1 >%2
goto :eof
::
:reverse
cscript /nologo hexdump.vbs -r <%2 >%3
:eof
Re: Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 21 Mar 2012 06:55
by miskox
Thank you. This does the job well.
Saso
Re: Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 21 Mar 2012 16:05
by aGerman
FC /B is the only suitable batch command to achieve that.
Small workaround (on Win7 "Run as admin" due to the FSUTIL):
Code: Select all
@echo off &setlocal
set "infile=a.exe"
set "outfile=a.txt"
cd /d "%~dp0"
if not exist "%infile%" goto :eof
set "tmpf=%temp%\#.tmp~"
del "%tmpf%" 2>nul
for %%i in ("%infile%") do (
set /a size=%%~zi || goto :eof
fsutil file createnew "%tmpf%" %%~zi >nul || goto :eof
)
setlocal EnableDelayedExpansion
set /a x=1
>"%outfile%" (
for /f "skip=1 tokens=1,2 delims=: " %%i in ('fc /b "%infile%" "%tmpf%"') do (
set /a y=0x%%i
for /l %%k in (!x! 1 !y!) do <nul set /p "=00 "
<nul set /p "=%%j "
set /a x=y+2
)
for /l %%i in (!x! 1 !size!) do <nul set /p "=00 "
echo(
)
del "%tmpf%"
The max. file size (Bytes) of the input has to be less than the numeric batch limit.
Regards
aGerman
Re: Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 23 Mar 2012 11:50
by miskox
aGerman, I am speechless. You are great. I'am still going thru the code to understand it. Though .vbs version works I like this, too - because it is just a little more pure DOS version.
One thing I noticed: CRLF are added at the end. Can this be avoided?
Thanks,
Saso
Re: Stream-IO (reading a file byte by byte) in DOS batch?
Posted: 23 Mar 2012 12:34
by aGerman
Of course it can. Remove line
Regards
aGerman