Solved

Reading text file in ASP 3.0

Posted on 2006-10-19
8
311 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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/…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now