aguldber
asked on
ASP using the TextStream Object
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.Fi leSystemOb ject")
%>
<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.AtEndOfS tream
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...
<%
<!-- 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.Fi
%>
<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.
<!-- While not the EOF read in each line of the file and write out the contents of the file -->
Do while not objFileTextStream.AtEndOfS
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...
ASKER
Adjusted points to 200
ASKER
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.Fi leSystemOb ject")
%>
<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.AtEndOfS tream
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>
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.Fi
%>
<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.
<!-- While not the EOF read in each line of the file and write out the contents of the file -->
Do while not objFileTextStream.AtEndOfS
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>
set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
should be
set objFSO = Server.CreateObject("Scrip ting.FileS ystemObjec t")
should be
set objFSO = Server.CreateObject("Scrip
ASKER
Added that to my code and it still comes up with the same error...
ASKER
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Fi leSystemOb ject")
%>
<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(ForR eading, 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.AtEndOfS tream
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>
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.Fi
%>
<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(ForR
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.AtEndOfS
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>
ASKER
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.Fi leSystemOb ject")
%>
<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(strpat h & "\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.AtEndOfS tream
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>
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.Fi
%>
<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(strpat
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.AtEndOfS
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>
ASKER
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_l edger")
' create a new file system object
set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
%>
<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(strpat h & "\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.AtEndOfS tream
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>
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_l
' create a new file system object
set objFSO = CreateObject("Scripting.Fi
%>
<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(strpat
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.AtEndOfS
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>
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