Solved

VBScript String Conversion

Posted on 2000-05-11
11
3,052 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

809 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