Solved

ASP using the TextStream Object

Posted on 1998-09-02
10
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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
 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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