Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to read an on line text file. 1

Status
Not open for further replies.

donmc4

Programmer
Jun 27, 2003
11
0
0
CA
I am wondering if it is possible to have VBScript read a text file that is on my web site i.e uploaded to my provider. I can't seem to get it to recognize where the file is with a line such as:

Set objTextFile = objFSO.OpenTextFile(" ForReading)

Does anyone know if this is possible to do?

Thanks
DonMc4
 
This should work ....
Do While objTextFil.AtEndOfLine <> True
MsgBox(&quot;Line Read: &quot; & objTextFil.ReadLine)
Loop
objTextFil.close
 
Actually my problem is how to point to a file that is on the web. I get an error meassage when I point using the http address listed in the original meassage even though it is the actual address. Any ideas?
 
Here is my complete code:
---------------------------------------------------
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
Sub btnGo_Click()

Dim objFSO, objTextFile, sRead, sReadLine, sReadAll,theCount

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

' Open file for reading.
Set objTextFile = objFSO.OpenTextFile(&quot; ForReading)

Do While objTextFile.AtEndOfStream = False
sReadLine = objTextFile.ReadLine
WhereColon = InStr(1, sReadLine, &quot;:&quot;)
Select Case Left(sReadLine, WhereColon - 1)
Case Cstr(&quot;TieRate&quot;)
Default = Right(sReadLine, Len(sReadLine) - WhereColon)
Exit Do
Case Else
Default = &quot;Error reading config&quot;
End Select
Loop
msgbox Int(Default)
objTextFile.Close

End Sub
</script>
---------------------------------------------------
I get an error meassage 'bad file name or number'. Anything obvious that I am doing wrong?
 
Sorry, I don't know where the semi-colon comes from but it isn't there when I view the source with IE.
 
Maybe something is adding the &quot;;&quot;. Try creating a variable and set it to the path and the use the variable.
Like this:
Const TriStateUseDefault = -2 TriStateTrue = -1 TriStateFalse = -0
strPath = &quot;*** NOTE ***
I noticed that posting this reply will add a &quot;;&quot; to the path, so don't add it into the code.
*** NOTE ***
Set objFile = objFSO.GetFile(strPath)
Set objTextStream = objFile.OpenAsTextStream(ForReading,TriStateUseDefault)
Do While objFileTextStream.AtEndOfStream <> True
.
.
Loop
objFileTextStream.Close
 
The semicolon is probably coming from the TGML. Try putting your code within the &quot;code&quot; &quot;/code&quot; tags (with square brackets instead of quotes obviously). Here's a test of a URL typed without a semi-colon:

not in a code block: &quot;
Code:
yes in a code block: &quot;[URL unfurl="true"]http://tek-tips.com&quot;[/URL]

You can also use the Preview Post button to see what your post is going to look like before sending it.

Yep indeedy! I previewed it, and the code block tags prevented the semi-colon from appearing. I also previewed it without &quot;Process TGML&quot; checked below, and no semi-colons showed up that way either.

- xift
 
Thanks xift. I'll keep this mind the next time I need to post code.
 
You can't use the FSO to reach anything that isn't part of the filesystem.

Alternatives to pull text from a web site include the Tabular Data Control and the XMLHTTP control.
 
Do you have time to explain how to do those two steps you have suggested? i.e. are they hard to learn etc...
 
The TDC was meant to pull comma or tab delimited &quot;tabular&quot; data from text files on the server and use it with IE's data binding facility to auto-generate tables and such.

It can easily be bent to other purposes by using its embedded Recordset object however, which is much like an ADO Recordset. Here is an example where I set the field delimiter to a junk value &HFD and the text qualifier (normally a '&quot;') to &HFE and the row delimiter (normally vbNewLine) to a &quot;|&quot; so that the whole file gets suck in as one record, newlines and all. This let me use it as a client-side HTML include.

You can just as easily leave the row delimiter alone and get back multiple records, or even use the other delimiters as they are meant to be used to get multiple fields in each Record of the Recordset.

data.htm
Code:
<html>
  <head>
    <title>Testing client-side include</title>
    <object id=objTDC
      classid=&quot;clsid:333C7BC4-460F-11D0-BC04-0080C7055A83&quot;>
      <param name=&quot;DataURL&quot; VALUE=&quot;data.txt&quot;>
      <param name=&quot;UseHeader&quot; VALUE=&quot;False&quot;>
      <param name=&quot;FieldDelim&quot; VALUE=&quot;&#FD&quot;>
      <param name=&quot;TextQualifier&quot; VALUE=&quot;&#FE&quot;>
      <param name=&quot;RowDelim&quot; VALUE=&quot;|&quot;>
    </object>
    <script language=&quot;VBScript&quot;>
      Sub window_onload()
        Dim objRS

        Set objRS = objTDC.recordset
        objRS.MoveFirst
        divInclude.innerHTML = objRS(0)
        Set objRS = Nothing
      End Sub
    </script>
  </head>
  <body>
    <p>Beginning</p>
    <div id=&quot;divInclude&quot;></div>
    <p>End</p>
  </body>
</html>
data.txt
Code:
<span style=&quot;background-color: blue; color: white&quot;>
This is a test of the emergency broadcasting system.<br>
This is only a test.</span>|
One advantage of the TDC is that you can get back records if you need them.

The XMLHTTP object can be used in a similar manner. Though intended to retrieve XML text it can grab almost anything. It can also send to the web server, simulation a form submit for example. There is no support for records as such however. Normally the data pulled back from the server is run through an XML parser object from the same library, and dealt with that way. Doesn't have to be though! You could just as easily walk though the blob of returned data with script code, use Split( ), or do what you will.

XMLHTTP would actually be cleaner for a client-side include because it DOES return such a text-blob.

I don't have a handy example, but the MSDN library online has more on both of these components.

 
I probably should have noted that my example retrieves all of the text not only into one record, but also one field of that record.

And like an ADO Recordset, the TDC Recordset supports the GetRows( ) method that you can use to pull the rows/fields into a 2-dimensional array if you are happier working with the data that way.

If you do want to use &quot;fields&quot; in your text file with TDC though, the data needs to truly be columnar. If each record has a different layout you might be better off setting the field delimiter to some &quot;impossible&quot; value (not in your text) and letting each row contain a single text field that you pull apart in your script code.

Note the power of the TDC however: you can use a header row as the first line and specify field names and data types. You can also filter on field contents and even sort the records.

Data binding lets you build some nice user interfaces for paging through data with little effort too.

Sorry to ramble on so, but I think the TDC, client-side ADO, and data binding are some of the least appreciated features of IE. Another reason why other browsers just don't cut it and never will: IE is practically an application platform in itself.
 
Thanks for your code 'dilettante' I finally implemented it and it works great. However, (and correct me if I'm wrong) I can't edit or write to the text file with this method right? Do you know of a method that would let me write to the text file in a similar way?

Thanks for your help

Don
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top