Solved

VBScript String Conversion

Posted on 2000-05-11
11
3,068 Views
Last Modified: 2010-05-18
I'm running into a strange block, and I need some help.  I want ot store .doc files in a database as blob fields, and I have ADO figured out sufficiently to do this.  My problem is in reading the file initially from the hard disk, and writing it later after I retrieve it again from the database.

Using Open <filename> for binary as #1, I never have any problems; I can read and write a Word 2000 document with no trouble as a byte array, preserving integrity as I go.  I also save the space in the database by writing an ascii style byte array, rather than a unicode twobyte system.

However...

I would really like to write this as a VBScript file, for ease of modification purposes.  Unfortunately, VBScript does not support the Open statement, so I tried to use the Scripting.FileSystemObject.  THis seems to read files fine, whether as ascii or unicode, into byte arrays.  The only way I can use the FileSystemObject to write, however, is to use Strconv to push the ascii blob back into a unicode format that the FileSystemObject can write properly.  If I don't, it writes garbage.  My biggest problem is that Microsoft has eliminated StrConv from it's list of supported functions for VBScript!!

I have tried writing the full unicode byte array to the database, and I have no trouble, other than that it takes up twice as much room.  I really want ot be able to send it as ascii.

Can anyone suggest a solution?  
0
Comment
Question by:NickHeitz
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 2800171
Try this, it is in VB using Scripting Runtime but should easily convert to VB script.

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso As FileSystemObject, f As File
Dim tsWord As TextStream
Dim tsRead As TextStream
Dim aryWord() As Byte

Private Sub Command1_Click()
  Set fso = CreateObject("Scripting.FileSystemObject")
  fso.CreateTextFile "c:\testbed\testnew.doc", True, False
  Set f = fso.GetFile("c:\testbed\testnew.doc")
  Set tsWord = f.OpenAsTextStream(ForWriting, TristateFalse)
  Set tsRead = fso.OpenTextFile("c:\testbed\test.doc", ForReading, True, TristateFalse)
  Do
    tsWord.Write tsRead.Read(1)
  Loop Until tsRead.AtEndOfStream
  tsWord.Close
  tsRead.Close
  Set f = Nothing
  Set fso = Nothing
End Sub

I have tried it with some word documents and it creates a perfectly openable copy of the document.

I obviously haven't saved the textstream to a database in the meantime but as you have that figured you should be able to use this code to solve your problem.
0
 
LVL 2

Expert Comment

by:Crin
ID: 2800209
Hello,

Some notes about Unicode...
Each symbol presented as two bytes. For common fonts first byte is 0 and second is ascii code of symbol. So for common fonts you can just skip zeros.

The problem starts when you receive 'really' unicode symbol... Convert two bytes to long, and if value is less than zero you just need to add 4096 to get appropriate ascii symbol.

So, final solution (or your function StrConv) could be following:

A = B1*256 + B2
If A<0 then
 A=A + 4096
end if

Now A consist of ascii code of symbol.

Sincerely yours,

Crin

PS. English is not my native language, so please ask for more explanations if needed.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2800447
Nick, you can set the number of characters read in each call to a higher figure <= 999999999 in fact so that you don't have to do it char by char. I have also tried it by reading the entire file into the aryWord array and then writing the contents back and it seems to work fine!

Private Sub Command1_Click()
  Set fso = CreateObject("Scripting.FileSystemObject")
  fso.CreateTextFile "c:\testbed\testnew.doc", True, False
  Set f = fso.GetFile("c:\testbed\testnew.doc")
  Set tsWord = f.OpenAsTextStream(ForWriting, TristateFalse)
  Set tsRead = fso.OpenTextFile("c:\testbed\test.doc", ForReading, True, TristateFalse)
  Do
    aryWord = tsRead.Read(9999999)
    tsWord.Write aryWord
  Loop Until tsRead.AtEndOfStream
  tsWord.Close
  tsRead.Close
  Set f = Nothing
  Set fso = Nothing
End Sub
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:NickHeitz
ID: 2803956
I thank you all for your comments, but I think I may not have clearly explained the point.  I know that by using TriStateFalse, I can get a solution that works, but the resulting byte array is twice as massive as if I were able to open the file with "Open <filename> for Binary as #number".  Thus, the blob inputted into the database takes up more room.  If I could use strconv in vbscript, it would be no problem.

Try this...open your documents using TriStateTrue, instead of TriStateFalse, or use the open statement.  YOu won't be able to write back the string to the file using the filesystemobject successfully, I believe.
0
 
LVL 2

Expert Comment

by:Crin
ID: 2804177
Hello Nick,

I want to post a code for custom StrConv function (let us name it mStrConv) but I need a little example from VB that worked before using MS StrConv function.

Could you show it?

My previous message just provided the way you can create such function in VBScript.

Sincerely yours,

Crin


0
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2815019
NickHeitx,
Try this site and see if it will help you. I stink at VBscript but anytime I need vbscript help, I get it from here:

http://www.vb-web-directory.com/vbscript/vbscript.shtml

-good luck


0
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2815021
0
 

Author Comment

by:NickHeitz
ID: 2816400
THanks for the guidance to web pages.  Unfortunately, I haven't been able to find reference to anything concerning the reasons for dropping support for strconv in vbscript, nor what the alternatives are.  Thanks.

Crin : It is a bit difficult to post here.  If you give me an email address, I can send something to you.
0
 
LVL 2

Accepted Solution

by:
Crin earned 400 total points
ID: 2816779
crin@mail.com

Sincerely yours,

Crin
0
 

Author Comment

by:NickHeitz
ID: 2964033
Comment accepted as answer
0
 

Author Comment

by:NickHeitz
ID: 2964034
I've solved the problem in a different way...use java.  Unfortunately, it just looked like the FileSystemObject was imperfect in its handling of ascii streams...if you come up with a manual way of converting ascii to unicode, drop me a line:
niheitz@hotmail.com
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question