• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

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.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
aguldber
Asked:
aguldber
  • 7
  • 2
1 Solution
 
MasseyMCommented:
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
 
aguldberAuthor Commented:
Adjusted points to 200
0
 
aguldberAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sybeCommented:
set objFSO = CreateObject("Scripting.FileSystemObject")

should be

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

0
 
aguldberAuthor Commented:
Added that to my code and it still comes up with the same error...
0
 
aguldberAuthor Commented:
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
 
MasseyMCommented:
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
 
aguldberAuthor Commented:
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
 
aguldberAuthor Commented:
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
 
aguldberAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now