How to restart TextStream.AtEndOfStream

I successfully run the code below to display a text file in a browser, line by line:

<% 
    Filename = "/pages/test.txt"
    Set FSO = server.createObject("Scripting.FileSystemObject")
    Filepath = Server.MapPath(Filename)

    Set file = FSO.GetFile(Filepath)
    Set TextStream = file.OpenAsTextStream(1, -2)  

    Do While Not TextStream.AtEndOfStream  
            Line = TextStream.readline
            Response.Write Line & "<br>"
    Loop 

    Set TextStream = nothing
    Set FSO = nothing
%>

I would like to run the loop Do While Not TextStream.AtEndOfStreamagain, right in front of the statement Set TextStream = nothing.

It turns out that I cannot "just" copy the loop Do Whileand place it under the first instance. More results from TextStreamno.

Is there a way to reset an object TextStreamto return to the beginning of the stream?

I could store the strings in an array and use this, but I wanted to see if there was a simpler route.

+4
source share
2 answers

, TextStream. Close TextStream . , . , -, , , .

' Create an array containing each line from the text file...
a = Split(file.OpenAsTextStream(1, -2).ReadAll(), vbCrLf)

For i = 0 To UBound(a)
    Response.Write a(i) & "<br>"
Next

' Repeat the process...
For i = 0 To UBound(a)
    Response.Write a(i) & "<br>"
Next

<br> :

strText = Replace(file.OpenAsTextStream(1, -2).ReadAll(), vbCrLf, "<br>")

Response.Write strText
Response.Write strText    ' Write it again
+4

TextStream

TextStream Visual Basic Script Runtime , .

  • , , .

  • - , ForWriting, ( ). ForAppending).

?

ADODB.Stream Object!

, ADODB ADODB.Stream, , , ( ) Position, .

- :

<%
Dim TextStream, Filename, Filepath    

Filename = "/pages/test.txt"
Filepath = Server.MapPath(Filename)

Set TextStream = Server.CreateObject("ADODB.Stream")
Call TextStream.Open()
TextStream.Type = adTypeText

Call TextStream.LoadFromFile(Filepath)

Do While Not TextStream.EOS
  Line = TextStream.ReadText(adReadLine)
  Call Response.Write(Line & "<br>")
Loop

'Reset stream back to start of the stream
TextStream.Position = 0

Do While Not TextStream.EOS
  Line = TextStream.ReadText(adReadLine)
  Call Response.Write(Line & "<br>")
Loop    

Call TextStream.Close()
Set TextStream = Nothing
%>

, () , ADODB.Stream. , , TextStream.Position = 0 Do While , . , , .


0

Source: https://habr.com/ru/post/1599159/


All Articles