6 Replies - 3914 Views - Last Post: 08 February 2013 - 02:03 PM Rate Topic: -----

#1 dongraves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 08

Use StreamReader in VBScript

Posted 08 February 2013 - 12:35 PM

I know that VBScript loves the File System Object, and I've used it numerous times to read/write files. But now I have the need to open a file using the StreamReader object instead. I need to pass the object to a canned function that expects the object to be an instance of StreamReader. (The function reads from the file using StreamReader syntax.) I've searched online for days now and can't find any mention of employing it. I have tried each of the Set statements below that have already been commented out:

Dim Rec
Dim Output
Dim objSR
'Set objSR = CreateObject("SystemIO.StreamReader")
'Set objSR = CreateObject("System.IO.StreamReader")
'Set objSR = CreateObject("StreamReader")
'Set objSR = CreateObject("System_IO.StreamReader")
'Set objSR = CreateObject("dotNET.System_IO.StreamReader")
'Set objSR = CreateObject("dotNET.System.IO.StreamReader")
'Set objSR = CreateObject("dotNET.SystemIO.StreamReader")
Dim objSIO
Set objSIO = CreateObject("System.IO")
Set objSR = objSIO.StreamReader("C:\what\StreamReader\TestRead.txt")

MsgBox("so far so good")

'objSR.Open("C:\what\StreamReader\TestRead.txt")
Output = ""
While Not objSR.EndOfStream
	Rec = objSR.ReadLine
	Output = Output & Rec & vbNewLine
Wend
objSR.Close()
Set objSR = Nothing
MsgBox(Output)



My TestRead.txt file is a simple text file containing 3 short lines in it to test the file access:

This is line 1.
This is line 2.
This is line 3.

The error message I get always mentions the attempt I have defined in the CreateObject("whatever") in the various attempts listed above. The error I get is always: "ActiveX component can't create object: 'whatever' Code: 800A01AD


It irks me that I can use the file system object in Visual Studio projects, but I can't use StreamReader in VBScript files...? Is that fair?

Is This A Good Question/Topic? 0
  • +

Replies To: Use StreamReader in VBScript

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8948
  • View blog
  • Posts: 33,544
  • Joined: 12-June 08

Re: Use StreamReader in VBScript

Posted 08 February 2013 - 12:51 PM

Quote

It irks me that I can use the file system object in Visual Studio projects, but I can't use StreamReader in VBScript files...? Is that fair?

Sure it's fair. .NET projects are .NET projects.. VBScript are _modeled_ after VB, but are not VB. Two very separate projects and frameworks.

In fact I vaguely remember MS sunsetting VBS and pushing for Windows Powershell.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3227
  • View blog
  • Posts: 10,843
  • Joined: 12-December 12

Re: Use StreamReader in VBScript

Posted 08 February 2013 - 01:07 PM

Only some .NET objects, and some of their methods and properties, are available to VBScript. For example,

Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "I love deadlines. I like the whooshing sound they make as they fly by."
s.Append_3 "and the rest."
wscript.echo s.Length
wscript.echo s.Capacity
wscript.echo chr(s.chars(0))
wscript.echo s.Replace("t", "d").Replace("l", "k").toString

Stack Overflow reference.

fso has a TextStream object which would be the nearest equivalent.

Otherwise, you might have to create your own .NET class and expose it to COM, acting as a wrapper for StreamReader.

.. are you sure it has to be a StreamReader..? Particularly as you are only handling a simple .txt file.

Or PowerShell as indicated by modi123_1 :)

This post has been edited by andrewsw: 08 February 2013 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 dongraves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 08

Re: Use StreamReader in VBScript

Posted 08 February 2013 - 01:36 PM

So the short answer is "No, you can't do that." Crud. VBScript was chosen because its syntax is so close to the scripting language used by Pervasive. So all the code surrounding our use of the Pervasive products use VBScript. It has been quite convenient to be able to port a function from one to the other and it requires no tweaking at all. But now we're growing into real .NET stuff and we're finding we can't do all the things we want to do. Thanks for your input. I don't have a good solution, but at least now I don't have to spend more time looking for a solution that doesn't exist. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8948
  • View blog
  • Posts: 33,544
  • Joined: 12-June 08

Re: Use StreamReader in VBScript

Posted 08 February 2013 - 01:39 PM

Quote

But now we're growing into real .NET stuff and we're finding we can't do all the things we want to do.

I would be curious if you could explain this..
Was This Post Helpful? 0
  • +
  • -

#6 dongraves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 08

Re: Use StreamReader in VBScript

Posted 08 February 2013 - 01:57 PM

I meant that comment with respect to how the code is written for the Pervasive scripting. Perhaps it should read: "But now we're growing into real .NET stuff and we're finding that we can do things (like use StreamReader) that we can't use in the legacy scripting languages to accomplish tasks we want to do." We just acquired Visual Studio last fall and now I've starting writing VB that is compatible with neither Pervasive's VB-ish nor VBScript. So I can't port the source code anymore like I used to between VBScript and Pervasive's scripting language -- RIFL.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8948
  • View blog
  • Posts: 33,544
  • Joined: 12-June 08

Re: Use StreamReader in VBScript

Posted 08 February 2013 - 02:03 PM

Ah.. gotcha. Well, yes.. that is a bummer that Pervasive isn't quite on the upswing of usable languages.

Thankfully there are nice people making the API and library jump for you!
http://cs.pervasive....s/entry758.aspx

and it looks like they have a data provider!
http://www.pervasive...8A05_ADONET.pdf
http://www.pervasive...donet32_v10.htm

somewhere abouts:
C:\program files\Pervasive Software\psql\bin\3.0\Pervasive.Data.SqlClient.DLL .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1