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

handling binary files in VBScript

2,777 views
Skip to first unread message

"Crash" Dummy

unread,
Jun 14, 2003, 8:48:28 PM6/14/03
to
I want to be able to edit some binary files using VBScript, but I can't read
and/or write successfully using the usual read/write methods. For example, if I
use a simple test script to copy a BMP file,

Set fso=CreateObject("scripting.FileSystemObject")
set reader=fso.opentextfile("old.bmp")
set writer=fso.createtextfile("new.bmp")
buffer=reader.readAll
writer.write(buffer)

I just end up with a new file full of zeroes. Is there any way to read/write
binary files using VBScript?
--
Dave "Crash" Dummy - A weapon of mass destruction
cr...@gpick.com
http://lists.gpick.com

Han

unread,
Jun 14, 2003, 9:53:33 PM6/14/03
to
Hi Crash

Just as a starting point.
You may want adodb.stream. For text file, I've tried something like this
with successful result. Unbelievably slow. See if this has any meaning for
you.

set strm1=createobject("adodb.stream")

With strm1
.Type = 1
.Open
.LoadFromFile "c:\x.bmp"
End With

bd=strm1.read
lb= lenb(bd)
msgbox lb

for i=1 to lb
bd2=bd2 & "x0" & hex(ascB(midB(bd, i)))
next

msgbox bd2

""Crash" Dummy" <dva...@deathstar.mil> wrote in message
news:#DpCXftM...@tk2msftngp13.phx.gbl...

"Crash" Dummy

unread,
Jun 14, 2003, 10:19:14 PM6/14/03
to
>Just as a starting point.
>You may want adodb.stream. For text file, I've tried something like this
>with successful result. Unbelievably slow. See if this has any meaning for
>you.

That's a start. Where can I get documentation on adodb, like methods, how to
write the data to a new file, etc.

"Crash" Dummy

unread,
Jun 14, 2003, 10:38:28 PM6/14/03
to
Okay, I got it. You're right. It is slow! I made the mistake of testing with a
151 KB image. :-)

Thanks!


Christoph Basedau

unread,
Jun 15, 2003, 6:28:02 AM6/15/03
to
am 15.06.03 02:48 sprach "Crash" Dummy dieses:

> I want to be able to edit some binary files using VBScript, but I can't read
> and/or write successfully using the usual read/write methods. For example, if I
> use a simple test script to copy a BMP file,
>
> Set fso=CreateObject("scripting.FileSystemObject")
> set reader=fso.opentextfile("old.bmp")
> set writer=fso.createtextfile("new.bmp")
> buffer=reader.readAll
> writer.write(buffer)
>
> I just end up with a new file full of zeroes. Is there any way to read/write
> binary files using VBScript?

The only Problem IMHO are zero-characters in the bin-file.
the ReadAll-Methode treates them as string-terminaters
and fills up the rest with zeros.
If you read charcter by character you won't have
this prob, but you loose speed.

this works for me to make an identical copy:

Set oFS = CreateObject("Scripting.FileSystemObject")
Set reader=oFs.opentextfile("old.bmp")
set writer=oFs.createtextfile("new.bmp")
While reader.AtEndOfStream <>true
writer.write reader.read(1)
Wend
Set oWS = CreateObject("WScript.Shell")
oWS.Run "new.bmp"


--
Gruss
Christoph

Bob Barrows

unread,
Jun 15, 2003, 11:28:47 AM6/15/03
to
"Crash" Dummy wrote:
>> Just as a starting point.
>> You may want adodb.stream. For text file, I've tried something like
>> this with successful result. Unbelievably slow. See if this has any
>> meaning for you.
>
> That's a start. Where can I get documentation on adodb, like methods,
> how to write the data to a new file, etc.

http://msdn.microsoft.com/library/en-us/ado270/htm/adostartpage1.asp

Bob


"Crash" Dummy

unread,
Jun 15, 2003, 12:35:00 PM6/15/03
to
Thanks! It's bookmarked.

Dave


Michael Harris (MVP)

unread,
Jun 15, 2003, 3:36:08 PM6/15/03
to
> The only Problem IMHO are zero-characters in the bin-file.
> the ReadAll-Methode treates them as string-terminaters
> and fills up the rest with zeros.
> If you read charcter by character you won't have
> this prob, but you loose speed.


Also, determining the file size and then doing

allData = tsFile.Read <size-of-file>

is faster that ReadAll and doesn't have the problem with enmbedded nulls
(0x00) bytes...


--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

"Crash" Dummy

unread,
Jun 15, 2003, 4:48:31 PM6/15/03
to
>Also, determining the file size and then doing

>allData = tsFile.Read <size-of-file>

>is faster that ReadAll and doesn't have the problem with enmbedded nulls
>(0x00) bytes...

Thanks! That's the best way yet. It takes care of the null bytes and leaves me
with data in a buffer that I can easily edit. What I am trying to do is change
the palette in a 16 color bitmap. This code fragment will give you an idea of
what I am doing:

buffer=ts.read(54)
buffer=buffer & newPalette
ts.skip(64)
buffer=buffer & ts.read(sz-118)

Then write the buffer to a new file, or back to the old.

TJS

unread,
Jun 15, 2003, 5:00:42 PM6/15/03
to
since folks are on the topic of binary files.....

does anyone know if the file type can be extracted from the header of the
binary code to determine the type of image (gif,jpeg, etc) ?

I would to be able to get the image type, when the image is stored in a ole
object field of an access database


thanks for any help

Christoph Basedau wrote in message ...

Han

unread,
Jun 15, 2003, 7:51:17 PM6/15/03
to
Hi Micahel and Crash

Did Christoph's example work for your bitmap? It didn't in my case. To my
experience, converting byte array into string sometimes fail. So I gave up
the way. Even for text file, if it has BOM, textstream scheme didn't work.

""Crash" Dummy" <dva...@deathstar.mil> wrote in message

news:OB6o893M...@TK2MSFTNGP10.phx.gbl...

"Crash" Dummy

unread,
Jun 15, 2003, 8:11:22 PM6/15/03
to
>Did Christoph's example work for your bitmap? It didn't in my case. To my
>experience, converting byte array into string sometimes fail. So I gave up
>the way. Even for text file, if it has BOM, textstream scheme didn't work.

Christoph's method worked, but it was very slow, as you would expect. The code
fragment I posted based on Michael's suggestion works beautifully and fast (for
a script). The complete test file to replace the palette in an image is below my
sig.


--
Dave "Crash" Dummy - A weapon of mass destruction
cr...@gpick.com
http://lists.gpick.com

dim palet(15)
'palet-----B,G,R
palet(0)="0,0,0"
palet(1)="0,0,80"
palet(2)="0,80,0"
palet(3)="0,80,80"
palet(4)="80,0,0"
palet(5)="80,0,80"
palet(6)="80,80,0"
palet(7)="C0,C0,C0"
palet(8)="80,80,80"
palet(9)="0,0,FF"
palet(10)="0,FF,0"
palet(11)="0,FF,FF"
palet(12)="FF,0,0"
palet(13)="FF,0,FF"
palet(14)="FF,FF,0"
palet(15)="FF,FF,FF"

Set fso=CreateObject("scripting.FileSystemObject")

Set myFile=fso.getFile("crash.bmp") '640x480x16 image
sz=myFile.size
set ts=myFile.openAsTextStream(1)

buffer=ts.read(54)

for i=0 to 15
color=split(palet(i),",")
for n=0 to 2
buffer=buffer & chr("&H" & color(n))
next
buffer=buffer & chr(0)
next

ts.skip(64)

buffer=buffer & ts.read(sz-118)

ts.close

set ts=myFile.openAsTextStream(2)
ts.write(buffer)
ts.close


Han

unread,
Jun 15, 2003, 8:44:15 PM6/15/03
to
That looks nice Crash.

By the way, for the textstream problem, I imagine it comes from my localized
OS, Korean. Even for the BOM problem, error(funny characters) occurs when I
use local characters. If all the code is only for some OSs or for your own,
it may be sufficient. I even changed my language option in control panel
without success.

""Crash" Dummy" <dva...@deathstar.mil> wrote in message

news:uzh4Sv5M...@TK2MSFTNGP11.phx.gbl...

Christoph Basedau

unread,
Jun 15, 2003, 9:12:47 PM6/15/03
to
am 15.06.03 23:00 sprach TJS dieses:

> since folks are on the topic of binary files.....
>
> does anyone know if the file type can be extracted from the header of the
> binary code to determine the type of image (gif,jpeg, etc) ?

Just take a look at a gif or jpeg in a hexviewer.
GIFs start with 'GIF87a' or 'GIF89a' (Ascii), jpegs start
with (hex): FF D8 FF E0 00 10 followed by (ascii) 'JFIF'
Bitmaps start with 'BM', the 11th Char is a '6'
Of course this is just the intro of the header.

> I would to be able to get the image type, when the image is stored in a ole
> object field of an access database

If you want to be absolutely sure, what kind of image is stored
and maybe what subtype too, you need to study headers more closely.
there's a lot information stored in a few bits concerning
color depth, image width, palette information and so on.
which is quite interesting for retrieving technical data
of images or searching for doublets, series..

--
gruss
Christoph

Message has been deleted

TJS

unread,
Jun 16, 2003, 2:45:12 PM6/16/03
to
would you happen to know of a script or wsc file which returns these values
from a long binary field ?


Christoph Basedau wrote in message ...

Todd Fiske

unread,
Jun 16, 2003, 3:31:16 PM6/16/03
to
On Sun, 15 Jun 2003 12:36:08 -0700, Michael Harris (MVP) <mik...@mvps.org>
wrote:

>> The only Problem IMHO are zero-characters in the bin-file.
>> the ReadAll-Methode treates them as string-terminaters
>> and fills up the rest with zeros.
>> If you read charcter by character you won't have
>> this prob, but you loose speed.
>
>
> Also, determining the file size and then doing
>
> allData = tsFile.Read <size-of-file>
>
> is faster that ReadAll and doesn't have the problem with enmbedded nulls
> (0x00) bytes...

Could you show me an example where nulls cause a problem when reading
binary
data? I have used Read and ReadAll with a number of binary files (ADO .adtg
files for one) and haven't had any problems.

- Todd

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Todd Fiske

unread,
Jun 16, 2003, 3:41:06 PM6/16/03
to
On Sun, 15 Jun 2003 14:00:42 -0700, TJS <nos...@here.com> wrote:

> does anyone know if the file type can be extracted from the header of the
> binary code to determine the type of image (gif,jpeg, etc) ?
>
> I would to be able to get the image type, when the image is stored in a
> ole object field of an access database
>
> thanks for any help

I just found a script that does this the other day. It actually digs into
many types of image files to determine their sizes, and allows resizing
somehow.

http://www.lewismoten.com/Items/Browse.asp?Path=%2FProgramming%2FvbScript

Todd Fiske

unread,
Jun 16, 2003, 3:49:22 PM6/16/03
to
On Mon, 16 Jun 2003 15:31:16 -0400, Todd Fiske <tfi...@mindspring.com>
wrote:

Gak! Please forgive the unwrapped lines. It's my first time using "M2"
and it apparently has its own ideas about formatting.

TJS

unread,
Jun 16, 2003, 5:12:22 PM6/16/03
to
hi
thanks

I've seen that website already and actually posted a link to it when I asked
for help on this earlier.

that is for reading an image from a file I'm having trouble reading it
from a long binary field in an access database

I can't get it to read the image code correct.


Todd Fiske wrote in message ...

Todd Fiske

unread,
Jun 16, 2003, 6:12:58 PM6/16/03
to
On Mon, 16 Jun 2003 14:12:22 -0700, TJS <nos...@here.com> wrote:

> hi
> thanks
>
> I've seen that website already and actually posted a link to it when I
> asked for help on this earlier.
>
> that is for reading an image from a file I'm having trouble reading it
> from a long binary field in an access database
>
> I can't get it to read the image code correct.

Woops, newbie errors. Don't know specifically about reading from a long
binary field other than to use GetChunk which you probably already know
too!

I also did see ReadAll returning lots of (but not all) null characters when
loading a bitmap file. I guess I must have always used Read(#) for that.

Message has been deleted
Message has been deleted
0 new messages