Work with binary files in VBSscript - read and write local and remote files
ActiveX RegEdit.   ActiveX User account Manager   Pure-ASP Upload
Export MDB/DBF from ASP   Active LogFile   WebChecker   ActiveX/ASP Multi Dictionary object
 See 
 also 
 IISTracer, real-time IIS monitor and logging tool.
 Huge ASP file upload with progress bar. 



 Top messages
 22.3.2003 19:18:41 
 Read and write SQL image data, store binary file to sql table. (nbsp;WSHDatabaseConversionVBScript)
 4.5.2002 9:16:43 
 Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook. (nbsp;ASP / ASP.NetWSHVBScriptEmail)
 12.6.2003 9:14:29 
 Download multiple files in one http request (nbsp;File & data transferVBScript)
 10.2.2005 
 Add an email account to a windows 2003 pop3 service using script (nbsp;VBAEmail)

 Work with binary files in VBSscript - read and write local and remote files 

 Areas>Languages>VBScript
 Areas>ASP / ASP.Net>Functions
 Areas>ASP / ASP.Net>File & data transfer
 Areas>WSH

Basics

     VBScript and JScript/JavaScript do not have native functions/commands to read and write files, because these languages were designed as "safe" client-side programming languages. Denied access to file system is it's primary feature.
     Server-side ASP code and applications has another work requirements, file system access (read/write files) is at first of them. The only way to work with files is to use built-in or external ActiveX or COM object.
     Microsoft solved this problem using FileSystemObject - but the object cannot read/write binary files (There is some work-around to use it to store binary files also - see code bellow - but you can only read text files with this object).
     Many people work on this simple task in C++, VBA, Delphi and other languages to create object, which let's you read binary data. One of this great objects is our ByteArray class (member of ScriptUtilities library), which let's you read and write binary files, work with binary data using blocks, convert it to/from Unicode string using more than 100 code pages, convert to hex string, etc.

      Please see pure-asp file upload with progress bar if you want to upload and save files from client-side.
      This article shows several ways to work with binary files on local and remote computers (with http/ftp) using free objects from Microsoft.

1. ADODB.Stream object 

     ADODB.Stream is a first object you can use to read/write text and binary files. The object is included in ADO 2.5 and later.

a) SaveBinaryData 

Function SaveBinaryData(FileName, ByteArray)
  Const adTypeBinary = 1
  Const adSaveCreateOverWrite = 2
  
  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")
  
  'Specify stream type - we want To save binary data.
  BinaryStream.Type = adTypeBinary
  
  'Open the stream And write binary data To the object
  BinaryStream.Open
  BinaryStream.Write ByteArray
  
  'Save binary data To disk
  BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function

b) SaveTextData

     ADODB.Stream let's you also save text data and let's you specify charset (codepage) for text-to-binary data conversion (against of Scripting.TextStream object). 

Function SaveTextData(FileName, Text, CharSet)
  Const adTypeText = 2
  Const adSaveCreateOverWrite = 2
  
  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")
  
  'Specify stream type - we want To save text/string data.
  BinaryStream.Type = adTypeText
  
  'Specify charset For the source text (unicode) data.
  If Len(CharSet) > 0 Then
    BinaryStream.CharSet = CharSet
  End If
  
  'Open the stream And write binary data To the object
  BinaryStream.Open
  BinaryStream.WriteText Text
  
  'Save binary data To disk
  BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function


c) ReadBinaryFile

Function ReadBinaryFile(FileName)
  Const adTypeBinary = 1
  
  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")
  
  'Specify stream type - we want To get binary data.
  BinaryStream.Type = adTypeBinary
  
  'Open the stream
  BinaryStream.Open
  
  'Load the file data from disk To stream object
  BinaryStream.LoadFromFile FileName
  
  'Open the stream And get binary data from the object
  ReadBinaryFile = BinaryStream.Read
End Function

d) ReadTextFile

Function ReadTextFile(FileName, CharSet)
  Const adTypeText = 2
  
  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")
  
  'Specify stream type - we want To get binary data.
  BinaryStream.Type = adTypeText
  
  'Specify charset For the source text (unicode) data.
  If Len(CharSet) > 0 Then
    BinaryStream.CharSet = CharSet
  End If
  
  'Open the stream
  BinaryStream.Open
  
  'Load the file data from disk To stream object
  BinaryStream.LoadFromFile FileName
  
  'Open the stream And get binary data from the object
  ReadTextFile = BinaryStream.ReadText
End Function

2. Scripting.FileSystemObject object 

     You can also use TextStream object from Scripting library to store binary data. This function may be useable on servers without ADO installed. This function uses BinaryToString from Convert a binary data (BinaryRead) to a string by VBS article.

a) SaveBinaryDataTextStream 

Function SaveBinaryDataTextStream(FileName, ByteArray)
  'Create FileSystemObject object
  Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
  
  'Create text stream object
  Dim TextStream
  Set TextStream = FS.CreateTextFile(FileName)
  
  'Convert binary data To text And write them To the file
  TextStream.Write BinaryToString(ByteArray)
End Function

3. WinHttpRequest (XMLHTTP, ServerXMLHTTP) and remote files. 

     Microsoft created several objects, which let's you read remote binary and text files. First of them was Microsoft.XMLHTTP - the object is using WinINET API, so there is no good idea to use it on server-side ASP script. Thanks to WinINET API, XMLHTTP has a great functionality - it supports any URL - http, https, ftp and gopher. The URL looks like protocol://usename:password@server/folder/file?parameters.

     Other MS HTTP objects are using new WinHTTP interface. There are three object you can use:
MSXML2.ServerXMLHTTP
WinHttp.WinHttpRequest.5
WinHttp.WinHttpRequest
    ServerXMLHTTP and WinHttpRequest objects has a lot of properties and methods, which let's you specify proxy, additional request headers and so on.
    You can download latest versions of WinHttp objects from msdn site, they are also included in some windows service packs and new windows versions.
    See also post binary data to URL from WSH/ASP/VBA/VBS.

a) BinaryGetURL 

Function BinaryGetURL(URL)
  'Create an Http object, use any of the four objects
  Dim Http
'  Set Http = CreateObject("Microsoft.XMLHTTP")
'  Set Http = CreateObject("MSXML2.ServerXMLHTTP")
  Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
'  Set Http = CreateObject("WinHttp.WinHttpRequest")
  
  'Send request To URL
  Http.Open "GET", URL, False
  Http.Send
  'Get response data As a string
  BinaryGetURL = Http.ResponseBody
End Function

     You can use ResponseText property to get the document data as a string (or BinaryToString VBS function).

 
 

See also for 'Work with binary files in VBSscript - read and write local and remote files' article:

     Download multiple files in one http request This article shows a way to download multiple files in one http request. It let's you send an HTML page along with image preview, prepare more files for download and send the files as one data stream. One request, one authentication per multiple files.
     Read and write SQL image data, store binary file to sql table. Store and read SQL image/binary data using functions in this article. You can store local or remote files in an sql table along with a description and other fields.
     Post binary data to URL from WSH/ASP/VBA/VBScript. Two methods to POST binary or string data to external URL and read results sent from a script on the URL.
     Convert a binary data (BinaryRead) to a string by VBS This article, demonstrates several versions of source VBS code you can use to work with binary data in ASP and convert the data to a String format.
     Create and work with binary data in ASP/VBScript Lets you convert/create binary data in ASP to use the data for BinaryWrite/BinaryRead.
     Write to text file from MS-SQL. Stored procedure which enables write text data directly to the file.

If you like this page, please include next link on your pages:
<A
 Href="http://www.motobit.com/tips/detpg_read-write-binary-files/"
 Title="Reading and writting binary and text
  files is a first task
  you will need to solve
  in server-side ASP. This article
  contains several VBS functions which
  let's you store data to
  local disk and read local
  or remote (http) files."
>Work with binary files in VBSscript - read and write local and remote files</A>

     IISTracer - IIS ISAPI real-time monitor IISTracer is a real-time monitoring tool for Microsoft IIS, which will show/log you what is happenning on IIS server right now. It let's you reveal problems with long-running scripts (.asp, .cgi, cfm...), hang-up states and low resource situations and lets you stop long-running requests (uploads/downloads).      ActiveX User account Manager - Set of simple objects for creating, deleting, and managing user accounts, groups, servers and domains in the Windows NT environment.
     Active log file - Hi-performance text file logging for ASP/VBS/VBA applications. Lets you create daily/weekly/monthly log files with variable number of logged values and extra timing and performance info.      ActiveX windows registry editor - Intuitive, easy to use COM interface to windows registry. Set of classes to read/enumerate/modify windows registry keys and values from ASP, VBS and T-SQL.
     ActiveX/ASP Multi Dictionary object - Free-threaded hi-speed dictionary algorithm with unique/nonunique keys (map/multimap). Connect to another dictionary object in the same process. Lock and Unlock methods to synchronize tasks (application scope). Share ASP Application/Session objects.      Export DBF/MDB from ASP - Conversion from recordset to MDB/DBF. Direct binary output of MDB or DBF files from ASP pages with one row of code.
     Pure-ASP upload - lets you upload files using Pure ASP VBS code (using multipart/form-data and input type=file).      ByteArray - Works with safearray binary data (VT_UI1 | VT_ARRAY) - save/restore binary data from disk, find, work with code pages, convert to string/hexstring(SQL).
     WebChecker - Checks http, https, ftp and gopher internet connections in regular intervals. Lets you monitor web site functionality (uptime). Enables restart or notification on problems.      HTTPLog ISAPI filter - Lets you log incomming/outgoing http header and document data to separate files. Monitor of IIS service input/output.

© 1996 – 2008 Antonin Foller, PSTRUH Software, e-mail help@pstruh.cz