Solved

VBScript String Conversion

Posted on 2000-05-11
11
3,000 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:NickHeitz
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:orbitaltech
Comment Utility
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
Comment Utility
0
 

Author Comment

by:NickHeitz
Comment Utility
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
Comment Utility
crin@mail.com

Sincerely yours,

Crin
0
 

Author Comment

by:NickHeitz
Comment Utility
Comment accepted as answer
0
 

Author Comment

by:NickHeitz
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now