Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Compressed Folders in WSH
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? 
WINDOWS SCRIPTING

Compressed Folders in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-12-29

    Table of Contents:
  • Compressed Folders in WSH
  • Creating a Binary file
  • Getting a Byte Array in VBScript
  • Adding and Extracting files

  • 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


    Compressed Folders in WSH - Getting a Byte Array in VBScript


    (Page 3 of 4 )

    We know at this point that we need a byte array to write a binary stream to a file.  VBScript doesn’t provide a method of creating one, but we can read a byte array from a dummy file that we create with the information that we would like in it.

    objStream.Open

    objStream.Type = adTypeBinary

    objStream.LoadFromFile strPath

    We need to reopen the Stream object by using its Open method again.  This time we’ll set its Type to binary so that it reads in a binary byte array.  Finally, we can use the LoadFromFile method to load the contents of our dummy file into the data stream.

    objStream.Position = 2

    arrBytes = objStream.Read

    Remember that we want to skip the first two unwanted bytes of information.  To do this we’ll use the Stream object’s Position property to set the cursor position to the third character.  Positions are zero-based so we’ll set the property value to 2.  The Read method is then used to return a byte array from the stream.

    objStream.Position = 0

    objStream.SetEOS

    objStream.Write arrBytes

    Now we need to write our byte array back to the file in binary form.  We can reuse the binary stream that we already have open by setting the cursor position back to the beginning and using the SetEOS method. This method sets the End Of Stream marker to the current cursor position.  By setting it to the beginning of the stream, we can effectively dump the existing stream data.

    objStream.SaveToFile strPath, adSaveCreateOverwrite

    objStream.Close

    Set objStream = Nothing

    Now you can use the SaveToFile method to write the stream to file in true binary fashion.  Notice that we’re now overwriting the dummy file.  We’re all finished, so you can close the stream and disconnect its object.

    Voila!  A series of workarounds has enabled us to create a true byte array and write a binary file in VBScript using only native objects.  A quick peek with the hex editor shows that our zip file is exactly like the original one that we inspected when we began.

    More Windows Scripting Articles
    More By Nilpo


       · Hello,I was looking for this a long time!Great job!
       · Thank you very much.
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





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