• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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