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("Scri pting.File SystemObje ct");
var objFile = objFSO.GetFile("c:\test.tx t");
var objTS=objFile.OpenAsTextSt ream();
var iCount=0;
while(!objTS.AtEndOfStream )
{
objTS.ReadLine();
iCount++;
}
WScript.Echo(objFile.Name + "contains" + iCount + "line(s).");
Thanks in advance,
Bill.
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("Scri
var objFile = objFSO.GetFile("c:\test.tx
var objTS=objFile.OpenAsTextSt
var iCount=0;
while(!objTS.AtEndOfStream
{
objTS.ReadLine();
iCount++;
}
WScript.Echo(objFile.Name + "contains" + iCount + "line(s).");
Thanks in advance,
Bill.
ASKER
bOlscott,
No it's for an intranet application and the text files will be located on the web server.
rgds,
Bill.
No it's for an intranet application and the text files will be located on the web server.
rgds,
Bill.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.tx t");
to
var objFile = objFSO.GetFile("test.txt") ;
and get the same error.
any ideas?
I've changed this line...
var objFile = objFSO.GetFile("c:\test.tx
to
var objFile = objFSO.GetFile("test.txt")
and get the same error.
any ideas?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
as your file is located on webserver,
Try This :
var objFile = objFSO.GetFile(Server.MapP ath("test. txt"));
Regards,
Sandip.
Try This :
var objFile = objFSO.GetFile(Server.MapP
Regards,
Sandip.
ASKER
Thanks to all for your help.
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