Results 1 to 29 of 29

Thread: Text File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    How do i open a text file and print to screen with ASP..

    G
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  2. #2
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    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
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    It will thanks.... could it be any smaller?
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  4. #4
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    unfortunatley not.

    and also, please change this line

    set myFile = f.OpenAsTextStream(ForReading, -2)

    to

    set ts = f.OpenAsTextStream(ForReading, -2)
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    mmmh ..its not working
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    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
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    set myFile = f.OpenAsTextStream(ForReading, -2)

    Whats f ???
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  8. #8
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    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
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    I'd already done that.. still dont work says object requied on this line

    set myFile = FSO.GetFile(App.Path & "\license.txt")
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  10. #10
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    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
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    New stuff ehh!!

    is this valid then
    set myFile = FSO.GetFile(filepath & "\license.txt")

    Becuase i get file not found.. it is there.?

    G
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  12. #12
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    take the foward slash out of license

    Ian
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    Tried it.. still didn't work
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  14. #14
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    could you do a response.write filepath & "license.txt" and post it here please.

    Ian
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    <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?
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    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.
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  17. #17
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    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

    Code:
    <%
    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
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    <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
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  19. #19
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    Yeah that will work. the code above is handy to have if you need the actual directory of the file
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    It worked! a bit long but ..
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    Why dont it print the line breaks and spaces in the license.txt
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  22. #22
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    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
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    How can i make this:

    Do While not ts.AtEndOfStream
    myText = myText & ts.ReadLine
    Loop

    Pickup spaces and breaks in text file?

    Regards Gary
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  24. #24
    Addicted Member tonyenkiducx's Avatar
    Join Date
    Oct 2000
    Location
    London England
    Posts
    147
    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)

  25. #25
    Addicted Member tonyenkiducx's Avatar
    Join Date
    Oct 2000
    Location
    London England
    Posts
    147
    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 "&nbsp;"

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    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
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  27. #27
    Addicted Member tonyenkiducx's Avatar
    Join Date
    Oct 2000
    Location
    London England
    Posts
    147
    You need to assign the result to a variable...

    mytext = replace(mytext, chr(13), "<BR>")

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    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>
    "Life isn't about finding yourself. Life is about creating yourself."
    --George Bernard Shaw

  29. #29
    Addicted Member tonyenkiducx's Avatar
    Join Date
    Oct 2000
    Location
    London England
    Posts
    147
    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....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width