Solved

Reading text file in ASP 3.0

Posted on 2006-10-19
8
303 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

13 Experts available now in Live!

Get 1:1 Help Now