Stream-IO (reading a file byte by byte) in DOS batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Stream-IO (reading a file byte by byte) in DOS batch?

#1 Post by miskox » 20 Mar 2012 15:13

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!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Stream-IO (reading a file byte by byte) in DOS batch?

#2 Post by foxidrive » 20 Mar 2012 17:15

Windows Scripting Host and VBS can probably be used to do that.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Stream-IO (reading a file byte by byte) in DOS batch?

#3 Post by foxidrive » 20 Mar 2012 17:19

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


miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Stream-IO (reading a file byte by byte) in DOS batch?

#4 Post by miskox » 21 Mar 2012 06:55

Thank you. This does the job well.

Saso

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

Re: Stream-IO (reading a file byte by byte) in DOS batch?

#5 Post by aGerman » 21 Mar 2012 16:05

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

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Stream-IO (reading a file byte by byte) in DOS batch?

#6 Post by miskox » 23 Mar 2012 11:50

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

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

Re: Stream-IO (reading a file byte by byte) in DOS batch?

#7 Post by aGerman » 23 Mar 2012 12:34

Of course it can. Remove line

Code: Select all

  echo(

Regards
aGerman

Post Reply