Solved

Reading text file in ASP 3.0

Posted on 2006-10-19
8
336 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

690 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