Link to home
Start Free TrialLog in
Avatar of billy21
billy21

asked on

Reading text file in ASP 3.0

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.
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

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
Avatar of billy21
billy21

ASKER

bOlscott,

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

rgds,

Bill.
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of billy21

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as your file is located on webserver,
Try This :

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

Regards,
Sandip.
Avatar of billy21

ASKER

Thanks to all for your help.