Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using VBS to open binary files

304 views
Skip to first unread message

RoscoeTruthless

unread,
Mar 23, 2003, 11:51:59 PM3/23/03
to
Hello all
I have been trying to track down an example of using VBS to open a
binary file. I have seen examples in VBA using the open statement and
the binary flag but I have not been able to find anything for VBS.
I want to make a VBS script that can open a binary file and perhaps
encode it using base64 or uuencode. I have the function for encoding the
binary data but the problem remains how to get the data to the function.
Any help would be great.
Thanks I'm a bit of a noob.
--
Roscoe
----------------------------
I bring nothing to the table
----------------------------

Mark_Pryor

unread,
Mar 24, 2003, 12:38:28 AM3/24/03
to

"RoscoeTruthless" <roscoet...@lycos.com> wrote in message
news:oawfa.34462$cB3.1...@nnrp1.uunet.ca...

> Hello all
> I have been trying to track down an example of using VBS to open a
> binary file. I have seen examples in VBA using the open statement and
> the binary flag but I have not been able to find anything for VBS.
> I want to make a VBS script that can open a binary file and perhaps
> encode it using base64 or uuencode. I have the function for encoding the
> binary data but the problem remains how to get the data to the function.
> Any help would be great.

Using VBScript this can all be done using MS Objects:

To cache the binary file, use the Adodb.Stream (v 2.5+)

To Base64 encode, use the Capicom.Utilities (v 2.0+)

Searching groups.google should turn up plenty of example code.

Back to the Oscars,
Mark Pryor

RoscoeTruthless

unread,
Mar 24, 2003, 12:48:41 AM3/24/03
to
Mark_Pryor wrote:
>
> Using VBScript this can all be done using MS Objects:
>
> To cache the binary file, use the Adodb.Stream (v 2.5+)
>
> To Base64 encode, use the Capicom.Utilities (v 2.0+)
>
> Searching groups.google should turn up plenty of example code.
>
> Back to the Oscars,
> Mark Pryor
What exactly is that? Adodb.Stream or Capicom.Utilities are these things
I have already or do they need to be added?
Sorry for the noob dialog

Helge Wunderlich

unread,
Mar 24, 2003, 3:40:41 AM3/24/03
to
On Mon, 24 Mar 2003 00:51:59 -0400, RoscoeTruthless
<roscoet...@lycos.com> wrote:

>I have been trying to track down an example of using VBS to open a
>binary file.

The FileSystemObject's Read method returns a string, but it does not
modify the data in any way, so you can easily convert the string to a
numerical format using the Asc() function on each character in the
string:

Set fso = CreateObject("Scripting.FileSystemObject")
set ts = fso.OpenTextFile("C:\winnt\explorer.exe")
Wscript.Echo "The first byte is : 0x" & Hex(Asc(ts.Read(1)))
ts.Close

I used this to write my own hex dump tool. It must be run with
cscript.exe unless you really like messageboxes (the regular
expression stuff is just to filter out nonprintable characters and is
probably far from complete):

Option Explicit
Dim fso, ts, count, TextLine, HexText
Dim re

Set re = new RegExp
Set fso = CreateObject("Scripting.FileSystemObject")
If Wscript.Arguments.Count <> 1 Then
wscript.echo "One parameter, please"
wscript.quit
End If
Set ts = fso.OpenTextFile(Wscript.Arguments(0), 1, False)

re.pattern = _
"[^A-Za-z0-9_\(\)\!\x22#、%&=\\\*',;:\?\/\$」\-\+\{\[\]\}>< ]"
re.Global = True

Do While Not ts.AtEndofStream
TextLine = ts.read(16)
HexText = ""
For count = 1 To Len(TextLine)
HexText = HexText & _
Right(0 & Hex(Asc(Mid(TextLine,Count,1))),2) & ":"
Next
TextLine = re.Replace (TextLine, ".")
HexText = Mid(HexText,1,Len(HexText)-1)
wscript.echo TextLine & Space(20 - Len(TextLine)) & HexText
Loop
ts.close

--
Helge Wunderlich
(Please remove the spam trap if you wish to send email)

RoscoeTruthless

unread,
Mar 24, 2003, 2:53:02 PM3/24/03
to
Helge Wunderlich wrote:
> The FileSystemObject's Read method returns a string, but it does not
> modify the data in any way, so you can easily convert the string to a
> numerical format using the Asc() function on each character in the
> string:
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> set ts = fso.OpenTextFile("C:\winnt\explorer.exe")
> Wscript.Echo "The first byte is : 0x" & Hex(Asc(ts.Read(1)))
> ts.Close

Thanks for your reply.
Just a few questions though. Can the FileSystemObject be then used to
write the hex data obtained form the above code?
Also I tried the aove code and it returned 4D as the first byte. I
opened explorer.exe with my hex editor and didnt even find 4D in the
whole file.
Could you please explain why this is.
I'm a big programing noob.
TIA for your help.

mayayana

unread,
Mar 24, 2003, 9:14:35 PM3/24/03
to
If an external component is appropriate to your needs see:
http://www.jsware.net/jsware/scripts.html (See Binary Ops component).
It's probably the easiest way to do what you want but requires
that the component be installed on any machine running the script.

--
--


RoscoeTruthless <roscoet...@lycos.com> wrote in message
news:oawfa.34462$cB3.1...@nnrp1.uunet.ca...

Helge Wunderlich

unread,
Mar 25, 2003, 2:45:43 AM3/25/03
to
On Mon, 24 Mar 2003 15:53:02 -0400, RoscoeTruthless
<roscoet...@lycos.com> wrote:
>Just a few questions though. Can the FileSystemObject be then used to
>write the hex data obtained form the above code?

Yes. All you need to do, is to convert the numerical byte value back
to a character using the Chr() function. For example, if you want to
write a zero, you use:

ts.Write Chr(0)

If your numbers are represented by a hex value in a string, you must,
of course, convert them to a numerical value first.

>Also I tried the aove code and it returned 4D as the first byte.

That's the correct result. All EXE files start with an uppercase "M",
which has the ASCII value 4D hex or 77 decimal.

>I
>opened explorer.exe with my hex editor and didnt even find 4D in the
>whole file.

I cannot explain it. I just looked at my Explorer.exe (I have Windows
2000 SP3). It contains several 4Ds. It even contains one instance of
the ASCII sequence "4D", so no matter how I search (text or hex), I
still find at least one.

There should not be any need to search. Simply looking at the very
first byte should be all you need. Which hex editor do you use ?

RoscoeTruthless

unread,
Mar 25, 2003, 9:02:38 PM3/25/03
to
Helge Wunderlich wrote:
>Which hex editor do you use


Something called binEdit.
Doesn't make any sense.

Helge Wunderlich

unread,
Mar 26, 2003, 3:54:53 AM3/26/03
to
On Tue, 25 Mar 2003 22:02:38 -0400, RoscoeTruthless
<roscoet...@lycos.com> wrote:

>Something called binEdit.

I found a freeware program by that name, written by Bob Peiffer. Is
that it ?

>Doesn't make any sense.

It really doesn't. I downloaded BinEdit and opened Explorer.exe with
it. Here's a screen capture from the result:

http://home.no.net/helgeww/temp/binedit.gif

As you can see, the first byte is definitely 4d.

RoscoeTruthless

unread,
Mar 26, 2003, 4:18:32 PM3/26/03
to
Helge Wunderlich wrote:
> I found a freeware program by that name, written by Bob Peiffer. Is
> that it ?
Yeah thats it.

>>Doesn't make any sense.
> It really doesn't. I downloaded BinEdit and opened Explorer.exe with
> it. Here's a screen capture from the result:

> As you can see, the first byte is definitely 4d.

You said that all exe files start with M (0x4D).
I have opend some otehrs and they all seem to start with M. Strange why
my exploer.exe file is different.
Mystery abounds.

Helge Wunderlich

unread,
Mar 27, 2003, 2:32:44 AM3/27/03
to
On Wed, 26 Mar 2003 17:18:32 -0400, RoscoeTruthless
<roscoet...@lycos.com> wrote:
>You said that all exe files start with M (0x4D).
>I have opend some otehrs and they all seem to start with M. Strange why
>my exploer.exe file is different.

But the sample script I showed proves that the first byte is indeed
4d.

What does your Explorer.exe look like, then ?

0 new messages