Solved

Reading text file in ASP 3.0

Posted on 2006-10-19
8
322 Views
Last Modified: 2008-01-09
How do you read a text file in asp 3.0.  I have a javascript book that explains the process but when I execute the code I get the error 'WScript is undefined'.

I did some research and discovered that WScript comes from something called Windows Scripting Host.  I installed version 5.6 of WSH but it made no difference.

This is the example from the book...

var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
var objFile = objFSO.GetFile("c:\test.txt");
var objTS=objFile.OpenAsTextStream();
var iCount=0;

while(!objTS.AtEndOfStream)
{
      objTS.ReadLine();
      iCount++;

}

WScript.Echo(objFile.Name + "contains" + iCount + "line(s).");

Thanks in advance,

Bill.
0
Comment
Question by:billy21
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17769408
billy21,

Is this for an Internet page?  Where is the asp file located and where is the text file located?  If the asp page is on a website and you wish to access a file on the client's computer (i.e. c:\temp\text.txt) then that won't work.  First, ASP is run on the server which does not have access to the drive.  In fact the ASP code is never sent to the browser.  Second, if the browser were to get code (i.e. javascript) security limitations prevent it from accessing the computer in that fashion.  Cookies are an example of allowable file access but the browser can't just access any file.

I hope I didn't misunderstand your question.  Please let me know if I did.  Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 6

Author Comment

by:billy21
ID: 17769639
bOlscott,

No it's for an intranet application and the text files will be located on the web server.

rgds,

Bill.
0
 
LVL 58

Accepted Solution

by:
amit_g earned 175 total points
ID: 17769699
Create a Test.asp file in the C:\InetPub\WWWRoor folder and have this in it ...

<%@ Language=JScript %>
<%
var objFSO = Server.CreateObject("Scripting.FileSystemObject");
var objFile = objFSO.GetFile("c:\test.txt");
var objTS=objFile.OpenAsTextStream();
var iCount=0;

while(!objTS.AtEndOfStream)
{
     objTS.ReadLine();
     iCount++;

}

Response.Write(objFile.Name + "contains" + iCount + "line(s).");
%>

Make sure to grant at least read access to IUSR_MachineName user on directory c:\ and file c:\test.txt
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17769747
Then using Server.CreateObject instead of WScript to create the FileSystemObject will work.  Amit's suggested code should be just what you want.  Let us know if you still have a problem though or if you have any questions.
0
 
LVL 6

Author Comment

by:billy21
ID: 17770453
Thanks that seems to get me further than I was.  But for some reason is telling me 'file not found' now.  I have a file called test.txt in both the c: root directory and in wwwroot.

I've changed this line...
var objFile = objFSO.GetFile("c:\test.txt");

to

var objFile = objFSO.GetFile("test.txt");

and get the same error.

any ideas?
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 175 total points
ID: 17770795
Change

var objFile = objFSO.GetFile("c:\test.txt");

to

var objFile = objFSO.GetFile("c:\\test.txt");

0
 
LVL 12

Expert Comment

by:sandip132
ID: 17771598
as your file is located on webserver,
Try This :

var objFile = objFSO.GetFile(Server.MapPath("test.txt"));

Regards,
Sandip.
0
 
LVL 6

Author Comment

by:billy21
ID: 17787088
Thanks to all for your help.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API not working 33 67
format nvarchar field as mm/dd/yyyy 4 78
Auto Submit on dropdown box 14 72
XML and version of MS XML Dom Document versions 5 13
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

837 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