Solved

ASP using the TextStream Object

Posted on 1998-09-02
10
430 Views
Last Modified: 2013-12-25
What I want to do is read though a text report and if the first character of the line is a 1 (a control character) I want to use the CSS page break property on a tag and then print the line. If the 1 isn't the first character of the line, I just want to print the line. This is for an Intranet site that everyone has IE 4.0 and our web sever is IIS 4.0. I have gotten the page break to work on a static page so that is not the issue. I have defined a class style for the page break and am using it with the <PRE> tag because I want to keep the spacing of the report. This report is actually generated on the mainframe and is FTPed over the the web server in a virtual directory. I have taken a stab at it but to no avail. I have enclosed the code below...

<%
<!-- create constants to make Open Statement easier to understand -->
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const TriStateTrue = -1
Const TriStateFalse = 0
Const TriStateUseDefault = -2
Const TriStateMixed = -2

<!-- create a new file system object -->
set objFSO = CreateObject("Scripting.FileSystemObject")
%>
<html>

<head>
<title>Page Break test</title>
<style type="text/css">
<!--
.pagebreak {page-break-before: always;}
-->
</style>
</head>

<body>
<%
<!-- use the newly created object to open the specified file -->
Dim objFileTextStream
set objFileTextStream = objFSO.OpenTextFile("test.txt", ForReading, TriStateUseDefault)

<!-- While not the EOF read in each line of the file and write out the contents of the file -->
Do while not objFileTextStream.AtEndOfStream
      Response.Write objFileTextStream.ReadLine
Loop
objFileTextStream.close
%>

Example of the page breaking
<pre>Page 1</pre>

<pre class="pagebreak"> Page 2</pre>

</body>
</html>

I was trying this first on a file that was in the same directory as the .asp file but I couldn't even get it to read and write the contents of that file, let alone one on a virtual directory. I know I'm close, could you help me get the functionality I am looking for...

Thanks in advance...
0
Comment
Question by:aguldber
  • 7
  • 2
10 Comments
 
LVL 10

Expert Comment

by:MasseyM
ID: 1854724
You cannot access files using the filesystemobject in virtual directories... You must know the physical path.  

As for the line, put this in your loop

ALine = objFileTextStream.ReadLine
If Left(1,ALine) = "1" then SendLine = "<PRE CLASS=pagebreak>" & ALine & "</PRE>"
Else
SendLine = "<PRE>" & ALine & "</PRE>"
End If
Response.Write SendLine


thsi will read the first character of the line and if it is a "1" then it will add the "pagebreak" style to the pre.. if not, it will be plain...


- Matt
0
 

Author Comment

by:aguldber
ID: 1854725
Adjusted points to 200
0
 

Author Comment

by:aguldber
ID: 1854726
In regard to the virtual path comment: how can I get the physical path so that I can do this for those files?

I have inserted the code you gave me and tried viewing the page. When I did this, the title bar of the browser is says error and the only thing it shows is:

270 (0x0000010e)

I have included the asp code below...

<%
<!-- create constants to make Open Statement easier to understand -->
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const TriStateTrue = -1
Const TriStateFalse = 0
Const TriStateUseDefault = -2
Const TriStateMixed = -2

<!-- create a new file system object -->
set objFSO = CreateObject("Scripting.FileSystemObject")
%>
<html>

<head>
<title>Page Break test</title>
<style type="text/css">
<!--
pagebreak {page-break-before: always;}
-->
</style>
</head>

<body>
<%
<!-- use the newly created object to open the specified file -->
Dim objFileTextStream
set objFileTextStream = objFSO.OpenTextFile("test.txt", ForReading, TriStateUseDefault)

<!-- While not the EOF read in each line of the file and write out the contents of the file -->
Do while not objFileTextStream.AtEndOfStream
      ALine = objFileTextStream.ReadLine
If Left(1,ALine) = "1" then SendLine = "<PRE CLASS=pagebreak>" & ALine & "</PRE>"
Else
      SendLine = "<PRE>" & ALine & "</PRE>"
End If
Response.Write SendLine
Loop
objFileTextStream.close
%>
</body>
</html>


0
 
LVL 28

Expert Comment

by:sybe
ID: 1854727
set objFSO = CreateObject("Scripting.FileSystemObject")

should be

set objFSO = Server.CreateObject("Scripting.FileSystemObject")

0
 

Author Comment

by:aguldber
ID: 1854728
Added that to my code and it still comes up with the same error...
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:aguldber
ID: 1854729
I'm going to open this up to see if someone can spot why this ASP code doesn't work... I do appreciate your effort on this, MasseyM...
0
 
LVL 10

Accepted Solution

by:
MasseyM earned 200 total points
ID: 1854730
Sorry, I had some things backwards.  This works:
<%
' create constants to make Open Statement easier to understand
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const TriStateTrue = -1
Const TriStateFalse = 0
Const TriStateUseDefault = -2
Const TriStateMixed = -2

' create a new file system object
set objFSO = CreateObject("Scripting.FileSystemObject")
%>
<html>

<head>
<title>Page Break test</title>
<style type="text/css">
<!--
pagebreak {page-break-before: always;}
-->
</style>
</head>

<body>
<%
' use the newly created object to open the specified file
Dim objFileTextStream
set objFileTextStream = objFSO.OpenTextFile("d:\massey\internet\www\test.txt", ForReading, TriStateUseDefault)

' While not the EOF read in each line of the file and write out the contents of the file

Do while not objFileTextStream.AtEndOfStream

TALine = objFileTextStream.ReadLine

If Left(TALine,1) = "1" then
      SendLine = "<PRE CLASS=pagebreak>" & TALine & "</PRE>"
Else
      SendLine = "<PRE>" & TALine & "</PRE>"
End If

Response.Write SendLine
Loop
objFileTextStream.close
Set obnFileTextStream = nothing

%>
</body>
</html>
0
 

Author Comment

by:aguldber
ID: 1854731
Okay, with a little tweeking of the code you most recently gave me, I have my test file being read which is on the same directory level as my ASP on the web server.

I have two remaining issues: 1.) how do I get to my virtual directory on this server? 2.) Currently the page breaking doesn't work when I print from the browser.

Note: I found out that IE4.0 puts a blank row between each set of <PRE> tags. To get around that, I only use a new set of <PRE> tags at the start of a new page ("1").

Here is my most recent code...

<%
' create constants to make Open Statement easier to understand
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const TriStateTrue = -1
Const TriStateFalse = 0
Const TriStateUseDefault = -2
Const TriStateMixed = -2

' find the path to the file
strpath = server.mappath(".")
' create a new file system object
set objFSO = CreateObject("Scripting.FileSystemObject")
%>
<html>

<head>
<title>Page Break test</title>
<style type="text/css">
<!--
pagebreak {page-break-before: always;}
-->
</style>
</head>

<body>
<%
' use the newly created object to open the specified file
Dim objF
Dim objFileTextStream
set objF = objFSO.GetFile(strpath & "\test.txt")

set objFileTextStream = objF.OpenAsTextStream(ForReading, TriStateFalse)

response.write "<Font size=""2""><PRE>"

' While not the EOF read in each line of the file and write out the contents of the file
Do while not objFileTextStream.AtEndOfStream

TALine = objFileTextStream.ReadLine

If Left(TALine,1) = "1" then
SendLine = "</PRE><PRE CLASS=pagebreak>" & TALine & "<BR>"
Else
SendLine = TALine & "<BR>"
End If

Response.Write SendLine
Loop
Response.Write "</PRE></Font>"
objFileTextStream.close
Set obnFileTextStream = nothing

%>
</body>
</html>
 
0
 

Author Comment

by:aguldber
ID: 1854732
I've resolved the page break problem and just need to know how to get to the virtual directory...

Here's my code...

<%
' create constants to make Open Statement easier to understand
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const TriStateTrue = -1
Const TriStateFalse = 0
Const TriStateUseDefault = -2
Const TriStateMixed = -2

' find the path to the file
strpath = server.mappath(".")
' create a new file system object
set objFSO = CreateObject("Scripting.FileSystemObject")
%>
<html>

<head>
<title>Page Break test</title>
<style type="text/css">
<!--
pagebreak {page-break-before: always;}
-->
</style>
</head>

<body>
<%
' use the newly created object to open the specified file
Dim strFirstTime
strFirstTime = "True"
Dim objFileTextStream
set objFileTextStream = objFSO.OpenTextFile(strpath & "\test.txt",ForReading, TriStateFalse)

response.write "<Font size=""2""><PRE>"

' While not the EOF read in each line of the file and write out the contents of the file
Do while not objFileTextStream.AtEndOfStream

TALine = objFileTextStream.ReadLine

If Left(TALine,1) = "1" AND strFirstTime = "False" then
SendLine = "</PRE><PRE CLASS=pagebreak>" & TALine & "<BR>"
Else
SendLine = TALine & "<BR>"
End If

strFirstTime = "False"
Response.Write SendLine
Loop
Response.Write "</PRE></Font>"
objFileTextStream.close
Set obnFileTextStream = nothing

%>
</body>
</html>

0
 

Author Comment

by:aguldber
ID: 1854733
I've got the whole thing working now... Thanks for your help...

Here's the answer...

<%
' create constants to make Open Statement easier to understand
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Const TriStateTrue = -1
Const TriStateFalse = 0
Const TriStateUseDefault = -2
Const TriStateMixed = -2

' find the path to the file
strpath = server.mappath("/general_ledger")
' create a new file system object
set objFSO = CreateObject("Scripting.FileSystemObject")
%>
<html>

<head>
<title>Page Break test</title>
<style type="text/css">
<!--
pagebreak {page-break-before: always;}
-->
</style>
</head>

<body>
<%
' use the newly created object to open the specified file
Dim strFirstTime
strFirstTime = "True"
Dim objFileTextStream
set objFileTextStream = objFSO.OpenTextFile(strpath & "\test1.txt",ForReading, TriStateFalse)

response.write "<Font size=""2""><PRE>"

' While not the EOF read in each line of the file and write out the contents of the file
Do while not objFileTextStream.AtEndOfStream

TALine = objFileTextStream.ReadLine

If Left(TALine,1) = "1" AND strFirstTime = "False" then
SendLine = "</PRE><PRE CLASS=pagebreak>" & TALine & "<BR>"
Else
SendLine = TALine & "<BR>"
End If

strFirstTime = "False"
Response.Write SendLine
Loop
Response.Write "</PRE></Font>"
objFileTextStream.close
Set obnFileTextStream = nothing
%>
</body>
</html>

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

747 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

16 Experts available now in Live!

Get 1:1 Help Now