Code Examples
  Home arrow Code Examples arrow Binary File, Array Scripting Secrets
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Binary File, Array Scripting Secrets
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-03-25

    Table of Contents:
  • Binary File, Array Scripting Secrets
  • Writing Binary Files with the FileSystemObject
  • Removing an array element
  • Adding Voice to Your Scripts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Binary File, Array Scripting Secrets


    (Page 1 of 4 )

    It’s time for another installment of Nilpo’s Scripting Secrets. In the third edition of my series featuring insider scripting tips, you’ll learn how to write binary files in two different ways, a useful trick when working with arrays, and how to bring your scripts to life with sounds and spoken text. Today’s article is mostly about having fun!

    Writing Binary Files – Method 1 (ADODB.Stream)

    I’ve been using the ADODB.Stream object to create binary files in WSH for quite some time now.  I discovered this method a while back while trying to find a way to create Compressed Folders natively.

    I began by opening a newly-created Compressed Folder in a standard hex editor.  I could see that the file was a simple run of twenty-two bytes as follows.

    50 4B 05 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    If I could find a way to write those twenty-two bytes to a file, I would be able to create an empty compressed folder by creating its binary contents directly.  This proved slightly difficult, because neither VBScript nor WSH provide a means of writing binary information to a file.

    strPath = "C:Zip.zip"

     

    Const adTypeBinary = 1

    Const adTypeText = 2

    Const adWriteChar = 0

    Const adSaveCreateNotExist = 1

    Const adSaveCreateOverwrite = 2

     

    With CreateObject("ADODB.Stream")

    I knew from previous programming experience that I would need an IStream object to write binary data. That's the programming object required to manipulate binary streams.  A little research showed that the ADODB.Stream object returned an IStream object, so I began playing around to see if I could get it to do what I wanted.

       .Open

       .Type = adTypeText

       .WriteText ChrB(&h50) & ChrB(&h4B) & ChrB(&h5) & ChrB(&h6)

       For i = 1 To 18

           .WriteText ChrB(&h0)

       Next

       .SaveToFile strPath, adSaveCreateNotExist

       .Close

    The ADODB stream provided two methods for writing files from the stream: one for binary and one for text.  Unfortunately, the binary method required a byte array as input.  VBScript, not being designed for binary use, does not provide any means of creating a true byte array, so I had to develop a workaround.

    I was able to write text to the stream character by character.  I used VBScript’s little-known ChrB function to write binary characters to the stream one at a time.  Saving the file and closing the stream wrote the binary data to the file as I had hoped.

       .Open

       .Type = adTypeBinary

       .LoadFromFile strPath

       .Position = 2

       arrBytes = .Read

       .Position = 0

       .SetEOS

       .Write arrBytes

       .SaveToFile strPath, adSaveCreateOverwrite

       .Close

    End With

    I thought that I had it figured out, until I tried to open the new compressed folder.  Windows politely informed me that the file was corrupt.  Examining the new file with the hex editor revealed the problem.  The WriteText method had added two unwanted bytes to the beginning of the file before it wrote my binary information.  There wasn’t any way to avoid this happening, so I needed a way to remove the unwanted bytes after creating the file.

    With the file created, I could change the ADODB Stream type to binary and read in the binary information from my file.  I could now manipulate the stream contents and write it back to the file as binary—something I couldn’t do before.

    An ADODB stream is similar to a record set in that you can read it a little bit at a time, and a cursor marks your position in the stream.  By manually setting the position to 2 and then reading the stream contents, I could read in the binary information I wanted while skipping the first two unwanted bytes.

    Then, I returned the cursor position to the beginning of the stream and used the SetEOS method to set a new End Of Stream marker.  This effectively clears the contents of the stream before writing back the cleaned-up binary information.

    More Code Examples Articles
    More By Nilpo


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT