How do i open a text file and print to screen with ASP..
G
Printable View
How do i open a text file and print to screen with ASP..
G
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
It will thanks.... could it be any smaller?
unfortunatley not.
and also, please change this line
set myFile = f.OpenAsTextStream(ForReading, -2)
to
set ts = f.OpenAsTextStream(ForReading, -2)
mmmh ..its not working
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
set myFile = f.OpenAsTextStream(ForReading, -2)
Whats f ???
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
I'd already done that.. still dont work says object requied on this line
set myFile = FSO.GetFile(App.Path & "\license.txt")
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
New stuff ehh!!
is this valid then
set myFile = FSO.GetFile(filepath & "\license.txt")
Becuase i get file not found.. it is there.?
G
take the foward slash out of license
Ian
Tried it.. still didn't work
could you do a response.write filepath & "license.txt" and post it here please.
Ian
<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?
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.
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
a bit if a bugger, but it worksCode:<%
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"
Ian
<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
Yeah that will work. the code above is handy to have if you need the actual directory of the file :)
It worked! a bit long but ..
Why dont it print the line breaks and spaces in the license.txt
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
How can i make this:
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
Loop
Pickup spaces and breaks in text file?
Regards Gary
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)
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 " "
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
You need to assign the result to a variable...
mytext = replace(mytext, chr(13), "<BR>")
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>
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....