Dim objArgs, i

Set objArgs = WScript.Arguments
if objArgs.Count < 1 then
    WScript.Echo "Usage: VScript HexDump.vbs <in_file>" & vbCRLF & "Dumps a file in hex format to <filename.hex>."
    WScript.Quit
end if

Const TypeBinary = 1
Const ForReading = 1, ForWriting = 2, ForAppending = 8

for i = 0 to objArgs.Count - 1
    Dim fileName, outFileName, sz, ofst, nbrOnLine, OfstAtBeginningOfLine, hexBuf, ascBuf
    fileName = objArgs(i)
    outFileName = fileName & ".hex.txt"
    set fsObject=Wscript.CreateObject ("Scripting.FileSystemObject")
    if fsObject.FileExists(outFileName) then
        WScript.Echo "File " & outFileName & " already exists, replacing..."
    end if
    set outFile = fsObject.CreateTextFile (outFileName)
    set inFile = fsObject.GetFile(fileName)
    sz = inFile.Size

    With CreateObject("ADODB.Stream")
        .Type = TypeBinary
        .Open
        .LoadFromFile fileName
        .Position = 0 ' could be any byte position
	ofst = 0
	nbrOnLine = 0
	ofstAtBeginningOfLine = 0
	hexBuf = ""
	ascBuf = ""
	do while ofst < sz
	    c = AscB(.Read(1))
	    hexBuf = hexBuf & LCase(Right("0" & Hex(c), 2)) & " "
	    If c < 32 or c > 127 Then
                ascBuf = ascBuf & "."
	    Else
	        ascBuf = ascBuf & Chr(c)
            End If
	    ofst = ofst + 1
	    nbrOnLine = nbrOnLine + 1
	    If nbrOnLine >= 16 Then
	        outFile.WriteLine LCase(Right("00000000" & Hex(ofstAtBeginningOfLine), 8)) & "  " & hexBuf & "  " & ascBuf
		ascBuf = ""
		hexBuf = ""
		nbrOnLine = 0
		ofstAtBeginningOfLine = ofst
	    End If
	loop
	.Close
        If nbrOnLine >= 0 Then
	    outFile.WriteLine LCase(Right("00000000" & Hex(ofstAtBeginningOfLine), 8)) & "  " & Left(hexBuf & "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ", 48) & "  " & ascBuf
        End If
    End With
    outFile.Close
    
    '~ Dim WshShell, Pipe
    '~ set WshShell = WScript.CreateObject("WScript.Shell")
    '~ set Pipe = WshShell.Exec("notepad " & outFileName)
next

WScript.Echo "Done"
WScript.Quit