Click to See Complete Forum and Search --> : Text File
gsc1ugs
Jan 30th, 2001, 10:14 AM
How do i open a text file and print to screen with ASP..
G
Ianpbaker
Jan 30th, 2001, 10:48 AM
Hi again
nice and easy this one
Dim FSO
Dim myfile
Dim ts
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set FSO = Server.CreateObject("scripting.FileSystemObject")
set myFile = FSO.GetFile("C:\test1.txt")
set myFile = f.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine & "<br>"
Loop
Hope it helps
Ian
gsc1ugs
Jan 30th, 2001, 11:00 AM
It will thanks.... could it be any smaller?
Ianpbaker
Jan 30th, 2001, 11:03 AM
unfortunatley not.
and also, please change this line
set myFile = f.OpenAsTextStream(ForReading, -2)
to
set ts = f.OpenAsTextStream(ForReading, -2)
gsc1ugs
Jan 30th, 2001, 11:11 AM
mmmh ..its not working
gsc1ugs
Jan 30th, 2001, 11:12 AM
this is what i have
Dim FSO
Dim myfile
Dim ts
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set FSO = Server.CreateObject("scripting.FileSystemObject")
set myFile = FSO.GetFile(App.Path & "\license.txt")
set ts = f.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine & "<br>"
Loop
gsc1ugs
Jan 30th, 2001, 11:33 AM
set myFile = f.OpenAsTextStream(ForReading, -2)
Whats f ???
Ianpbaker
Jan 30th, 2001, 03:07 PM
Dohh, sorry mate I screwed up again, it's been one of those day's. change it to
set ts = myFile.OpenAsTextStream(ForReading, -2)
Ian
gsc1ugs
Jan 31st, 2001, 04:06 AM
I'd already done that.. still dont work says object requied on this line
set myFile = FSO.GetFile(App.Path & "\license.txt")
Ianpbaker
Jan 31st, 2001, 04:19 AM
just seen what is worng. App.path is not a valid function in asp, you need to use the server variable
filepath = request.servervariables("APPL_PHYSICAL_PATH")
that returns the physical path of the current file
Ian
gsc1ugs
Jan 31st, 2001, 04:24 AM
New stuff ehh!!
is this valid then
set myFile = FSO.GetFile(filepath & "\license.txt")
Becuase i get file not found.. it is there.?
G
Ianpbaker
Jan 31st, 2001, 04:27 AM
take the foward slash out of license
Ian
gsc1ugs
Jan 31st, 2001, 04:36 AM
Tried it.. still didn't work
Ianpbaker
Jan 31st, 2001, 04:41 AM
could you do a response.write filepath & "license.txt" and post it here please.
Ian
gsc1ugs
Jan 31st, 2001, 04:51 AM
<b>C:\Inetpub\wwwroot\license.txt</b>
There you go...
oops i may of found it... i'll let you know.. hold on a minute.. filepath is only doing C:\Inetpub\wwwroot\ and not
C:\Inetpub\wwwroot\inetcost\ .. why's that.?? thats the filepath?
gsc1ugs
Jan 31st, 2001, 04:53 AM
Now this dont work after all that... we'll get there yet
<textarea name="license" cols="80" rows="15" wrap="VIRTUAL" READONLY >
<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set FSO = Server.CreateObject("Scripting.FileSystemObject")
filepath = Request.ServerVariables("APPL_PHYSICAL_PATH")
set myFile = FSO.GetFile(filepath & "inetcost\license.txt")
set ts = myFile.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine & "<br>"
Loop
%>
</textarea>
This is everything... it fills in the <textarea> tag.
Ianpbaker
Jan 31st, 2001, 05:23 AM
right, i thought so, i'm using the wrong server variable.
I've just looked at all of them and the one you need is PATH_TRANSLATED. unfortunatley this also includes the filename of the asp page as well so you need to play with it a bit
<%
Dim blnfound
Dim filepath
Dim filename
Dim icount
'get the full path
filepath = request.servervariables("PATH_TRANSLATED")
blnFound = True
icount = 1 'start at the beguining of the string
While blnfound = True
'find a \ in the file path
if InStr(icount,filepath,"\") >= 1 Then
'set the counter to look for another "\" after the one it has just found
icount = instr(icount,filepath,"\") + 1
else
' no more "\"
blnfound = False
End If
wend
'start at the begining and grab everything up to the last "\"
filename = Mid(filepath,1,icount - 1) & "license.txt"
a bit if a bugger, but it works
Ian
gsc1ugs
Jan 31st, 2001, 05:32 AM
<textarea name="license" cols="80" rows="15" wrap="VIRTUAL" READONLY >
<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set FSO = Server.CreateObject("Scripting.FileSystemObject")
filepath = Request.ServerVariables("APPL_PHYSICAL_PATH")
set myFile = FSO.GetFile(filepath & "inetcost\license.txt")
set ts = myFile.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
Loop
Response.Write myText
%>
</textarea>
This works but strips out all spaces and breaks in text file
Cheers Ian
Ianpbaker
Jan 31st, 2001, 05:38 AM
Yeah that will work. the code above is handy to have if you need the actual directory of the file :)
gsc1ugs
Jan 31st, 2001, 05:39 AM
It worked! a bit long but ..
gsc1ugs
Jan 31st, 2001, 05:44 AM
Why dont it print the line breaks and spaces in the license.txt
Ianpbaker
Jan 31st, 2001, 05:46 AM
it's only 11 lines of code. stick it in a function, then in a include file and hey presto you have your own asp app.path :)
Ian
gsc1ugs
Jan 31st, 2001, 06:11 AM
How can i make this:
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
Loop
Pickup spaces and breaks in text file?
Regards Gary
tonyenkiducx
Feb 1st, 2001, 08:32 AM
To a save a lot of messing around with filepaths, use this bit of code.
path = server.MapPath("../images/wierddirectory/buriedinthemiddleofnowhere")
Set SaveFile = server.CreateObject("scripting.filesystemobject")
set imagefile = savefile.CreateTextFile(path & "\" & filename)
tonyenkiducx
Feb 1st, 2001, 08:34 AM
Ohh, and to pick up line breaks do this..
Replace (mytext, chr(13), "<BR>")
And just replace chr(13) with the space chr(Ive forgotten what it is) and "<BR>" with " "
gsc1ugs
Feb 1st, 2001, 08:41 AM
Get this error when using )
Cant use parantheses when calling a sub, taking them away doe'snt read the spaces or breaks. chr(10) i think is a space.
g
tonyenkiducx
Feb 1st, 2001, 08:49 AM
You need to assign the result to a variable...
mytext = replace(mytext, chr(13), "<BR>")
gsc1ugs
Feb 1st, 2001, 09:20 AM
Still wont work..
<textarea name="license" cols="80" rows="15" wrap="VIRTUAL" READONLY>
<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set FSO = Server.CreateObject("Scripting.FileSystemObject")
'get the full path*****************************************
getFileAndPath = filePath
'************************************************************
set myFile = FSO.GetFile(getFileAndPath)
set ts = myFile.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
Loop
myText = replace(myText, chr(10), "s")
myText = replace(myText, chr(13), "z")
Response.Write myText
%>
</textarea>
tonyenkiducx
Feb 1st, 2001, 09:31 AM
I cut and pasted that code, and ran it(With a new filename) and it works fine. If theres a problem remember to give an error message, or I dunno whats wrong with it. The problem with the "no parenthases" error is normally that one of your variables is empty, so check myText actually has something in it. I realise its not going to be that, but I cant think of anything else that may be wrong with it....
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.