Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenTextFile and Read All

Status
Not open for further replies.

goober808

Programmer
Oct 14, 2003
27
US
Please help!!! i just started with ASP programming and have no clue what i am doing wrong. I have a text file located on our server's C: drive that i am attempting to open, read all text, and output text onto page. BUT when i use the below code, nothing is being displayed on the page. Please help.

Code:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt"), 1)
Response.Write(f.ReadAll)
f.Close

Set f=Nothing
Set fs=Nothing
%>

Thanks in advance.....
 
>[tt]Set f=fs.OpenTextFile(Server.MapPath("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt"), 1)[/tt]

Do this instead.

[tt]Set f=fs.OpenTextFile("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt", 1)[/tt]

And that perform strange and that you are sure the file at that location and that you are sure there is no permission problem, then it probably is a file in the format of default unicode. In that case, try this instead. (-2 signifies opening the file using system default format.)

[tt]Set f=fs.OpenTextFile("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt", 1,true,-2)[/tt]

 
Thanks Tsuji for your help, i tried using both coding, but to my dismay... it did not work.

To make sure that i am coding the right path to the text file i have used the below code:

Code:
if fs.FileExists("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt")=true then
  response.write("File exists!")

My output is: File exists..... so i know i'm hitting the right path

HOWEVER, when i attempt to do a:

Code:
Set fso = Server.createobject("Scripting.FileSystemObject")
Set FilePath = server.mappath("LPWL05.txt")

Response.Write FilePath

the file path outputted is:

C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\LPWL05.txt

Notice that the folder "Specs" is not included when i attempt to do a server.mappath.

I made sure that all the necessary text files and folders that are being accessed have the authority. And now, i'm just completely stuck. Am i missing something big here or am i just not understanding anything? Thank you for taking the time to help me!!!!
 
[1]
>[tt][red]Set[/red] FilePath = server.mappath("LPWL05.txt")[/tt]
It would be an runtime error. This instead.
[tt]FilePath = server.mappath("LPWL05.txt")[/tt]
[2]
However if you are not looking for path relative to your site, but "outside" of your site root, there would be no point to use server.mappath---and it can give wrong return as well---especially you know the absolute path.
[3]
I am not convinced though despite your describing all your tests which seem all well-reasoned.
Try this short asp (can you keep the exact script?)
[tt]
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.fileexists("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt") then
Set f=fs.OpenTextFile("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt", 1,true,-2)
Response.Write escape(f.ReadAll)
f.Close
set f=nothing
else
response.write "Cannot find the file."
end if
Set fs=Nothing
%>
[/tt]
If the .txt file is actually some other formatted file such as binary, .readall is thereby not trustworthy. (Also, if it does not produce sensible result, alter the 4th parameter to -1 as described earlier.)
 
Try this.. this always works for me

Code:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
tmpStr = fs.opentextfile(server.mappath("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt")).readall

response.write tmpStr
%>

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
goober808, are you trying to read a file on the remote server or on the local PC?

First, Server.MapPath requires a virtual path, you cannot specify C:\ as that would be absolute.

If you want the script to read a file local to the client machine (not the server) then you need to use an ActiveX object to execute the code client-side and you would not user server.CreatObject as that executes it server-side.

If indeed you are trying to read a file on the server then ditch the MapPath as tsuji shows above and use an absolute path to the file.

As tsuji said you may run into file permission problems also. Your ASP code runs with permissions under the IUSR_machinename account and that account does not typically have full rights to folders outside of the IIS server unless you specifically assign them so you might run into permission problems reading the file.

The best test is to set a file to read inside the web folders to ensure the code works then to point to the actual file and test again. If it works in the first instance but fails in the second you know it is a security issue. If you do not test locally first then you do not know if it is permissions or code causing the problem.

Good luck.




Stamp out, eliminate and abolish redundancy!
 
Thanks everyone for your valuable input!

i hate to bring bad news.... but i still am not getting any output on my page. I have double checked my authority through IIS... I have even given everyone and anyone full authority to the file and folders. And at this point i have no idea why nothing outputs.

I've included, below, my entire ASP page. Maybe one of you can see an error in my coding that i could not. the coding i have added is indicated in Red.

Code:
<%@ Language=VBScript %>


<%
	Option Explicit
	Response.Expires = -1
	Response.Buffer = True
%>
<!-- #include file="../si_include/General.asp" -->
<!-- #include file="ReportHandler.asp" -->
<!-- #include file="../Menus/MenuProcessor2.asp"" -->
<%
	dim iErrWnd
	dim vCarrierImg
	iErrWnd = Request.QueryString("ErrWnd")
	if iErrWnd = "" then
		iErrWnd = 0
	end if
	On Error Resume Next
%>
<html>

<head>
<title><%=gstrSiteTitle%> - Case Run Messages</title>
<link REL="stylesheet" href="../VIP.css">
</head>
<%
If iErrWnd = 0 And Not gbSideMenus Then
	Response.Write "<script language=""JavaScript"" src=""../si_include/toolbar.js""></script>"
	Response.Write "<script language=""JavaScript"" src=""../Menus/MenuProcessor.asp?xml=Messages""></script>"
End If
%>

<body leftMargin="0" topMargin="0" MARGINWIDTH="0" MARGINHEIGHT="0">

<%
If iErrWnd = 0 And gbSideMenus Then
	Response.Write "<table border=0 cellpadding=0 cellspacing=0><tr valign=""top""><td class='MenuStrip'>"
	vDrawMenu "../Menus/MainMenu.xml", Nothing
	Response.Write "</td><td>"
End If
%>

<% If iErrWnd = 0 And Not gbSideMenus Then %>
<script language="JavaScript">
<!--// Hide from old browsers
var ToolBar_Supported = ToolBar_Supported;
if (ToolBar_Supported != null && ToolBar_Supported == true)
{
	drawToolbar();
}
//-->
</script>
<% End If %>

	<%if iErrWnd = 1 then
		vCarrierImg = "../" & oIllus("Plan.Carrier") & "/close.gif" 
	%>

		<table width="100%" border="0" cellpadding=2 cellspacing=2 ID="Table1">
		<tr>
			<td>An error occured proceessing your illustration. Close this window to return to your work:</td>
			<td align=right><a href="JavaScript:window.close()"><img src=<%=vCarrierImg%> border="0" style="filter: chroma(color:white)" alt="Click to return to work"></a></td>
		</tr>
		</table>
	<%end if 


	if iErrWnd = 0 then
		vBuildReportHeader "LPWL05", "Product Specifications"
	end if

	%>

[COLOR=red]
<%
Response.Write("<p align= center> <font size=3 face='Times New Roman'>PACIFIC GUARDIAN LIFE INSURANCE COMPANY, LIMITED<br>HORIZON JUNIOR+ & FLEX GUARANTEE<br>FORM NUMBER: LPWL05</font></p>")
Response.Write("<p align= center> <font size=3 face='Times New Roman'>PRODUCT SPECIFICATIONS</font></p>")

Set fs=Server.CreateObject("Scripting.FileSystemObject")

if fs.fileexists("C:\Program Files\SunGard Insurance Systems\VIPONLINE\PresASP\Specs\LPWL05.txt") then

    Set f=fs.OpenTextFile("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt", 1, true, -1)
    Response.Write escape(f.ReadAll)
    f.Close

    set f=nothing

else

    response.write "Cannot find the file."

end if

Set fs=Nothing

%>
[/color red]

thanks for all your help and patience with my minimal programming skills. i really appreciate it.
 
Try and remove the include files to see if anything displays on the screen.. they may be the cause.. I cannot see anything else jumping out at me at first glance.

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Gorkem,

i tried removing the "include" files, but that didn't solve my problem.... :( thanks for your input, though... :) I must be missing something.... somewhere... i just don't know where.

i'll keep on trying.....

thanks!!
 
Try pointing to a folder within the web site and see if it operates before trying it on folders outside of the IIS folders. Folders outside of the IIS web server security context are much more difficult to get access to so it is easier to verify the code works applying it against a web folder than one outside.


Stamp out, eliminate and abolish redundancy!
 
theniteowl,

i know this is a stupid question.... BUT when you say point to a folder, do you mean doing a GetFolder? sorry, i need all the help i can get!!! :)

 
Nope, I mean point to a folder that is part of the WEB site, not just a folder out on the C: drive.
Like a sub folder from your script file for instance.
The IUSR_machinename account inherantly more access in the web folders than it does in folders elsewhere on the drive.



Stamp out, eliminate and abolish redundancy!
 
OH, i see.... thanks, i'll give it a try & i'll let you know. thanks!
 
That this thing out
[tt] [red]'[/red]On Error Resume Next[/tt]
and then either declare all variables or take out option explicit for a quick test. See what error message you get.
 
This line may be problematic as well:
Code:
Set f=fs.OpenTextFile("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt", 1, true, [COLOR=red]-1[/color])


First test. In the same folder as your script create a file called test.txt with some random info in it for a reading test then use this block of code for testing it.
Code:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
  Dim filepath
  filepath = Server.MapPath("text.txt")
  if fs.fileexists(filepath,1) then
    Set f=fs.OpenTextFile(filepath, 1)
      Response.Write f.ReadAll
      f.Close
    Set f = Nothing
  else
    response.write "Cannot find the file."
  end if
Set fs=Nothing
%>

If that code works then try it with a file in the other folder with this code:
Code:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
  Dim filepath
  filepath = "C:\Program Files\SunGard Insurance Systems\VIPONLINE\PresASP\Specs\LPWL05.txt"
  if fs.fileexists(filepath) then
    Set f=fs.OpenTextFile("C:\Program Files\SunGard Insurance Systems\VIP\VIPONLINE\PresASP\Specs\LPWL05.txt", 1)
      Response.Write f.ReadAll
    f.Close
    set f=nothing
  else
    response.write "Cannot find the file."
  end if
Set fs=Nothing
%>

If the first code works and the second one does not then it is a permissions problem. Take a look at this link:
It is specifically talking about problems sending CDONTS messages but the permissions problem is the same.



Stamp out, eliminate and abolish redundancy!
 

OKAY.... after hours of trying everything and anything.... i FINALLY figured it out. i can't really specifically explain what i did to make it work.... BUT what ever i did it works. goodness.. i thought i was having a brain fart.

ANYWAYS...... this is the coding i used that made it work:

Code:
dim fs,f,flr,coverage

set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.mappath("LPWL05.txt"), 1)

do while f.AtEndOfStream = false
coverage=f.ReadLine
Response.Write(coverage)
Response.Write("<br>")
loop

f.close
set f=nothing
set fs=nothing

It seemed as if two things were holding me back....
1. f.ReadAll for some reason, was not working properly.
2. instead of having the txt file exist in a separate folder, i needed to have the txt file reside where my asp existed.

Thank you for all your patience and advice.... i couldn't have figured it out without your help.... everyone helped me understand ASP a little better.



 
>1. f.ReadAll for some reason, was not working properly.

Never heard of it. But if you format the output to browser with br tag, sure you can do it that way. The readall failing, it takes a lot of imagination to jump to that conclusion.
>2. instead of having the txt file exist in a separate folder, i needed to have the txt file reside where my asp existed.
So you have afterall problem of existence and permission and you still decide to have your misuse of "on error resume next" hanging there!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top