Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

READ TEXT FILE BASED ON VARIABLE 1

Status
Not open for further replies.

penguinspeaks

Technical User
Nov 13, 2002
234
US
I want to read a text file and display it on webpage. The file to display is based on a variable passed in a querystrind.
I am trying the following code, but getting an error: Expected literal constant

/STORY/STORY3.ASP, line 155

Const Filename = VTX

Here is my code:
Code:
VID = REQUEST.FORM("TITLE_")

Dim VTX

VTX = VID&".TXT"
'RESPONSE.WRITE(VTX)
Const Filename = VTX
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")

' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)

if FSO.FileExists(Filepath) Then

    ' Get a handle to the file
    Dim file    
    set file = FSO.GetFile(Filepath)

    ' Get some info about the file
    Dim FileSize
    FileSize = file.Size

    'Response.Write "<p><b>File: " & Filename & " (size " & FileSize " )</b></p>
     '              "<hr>"
    'Response.Write "<pre>"

    ' Open the file
    Dim TextStream
    Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)

    ' Read the file line by line
    Do While Not TextStream.AtEndOfStream
        Dim Line
        Line = TextStream.readline
    
        ' Do something with "Line"
        Line = Line & vbCRLF
    
        Response.write Line 
    Loop


    Response.Write "</pre><hr>"

    Set TextStream = nothing
    
Else

    Response.Write "<h3><i><font color=red> File " & Filename &_
                       " does not exist</font></i></h3>"

End If

Set FSO = nothing

It appears that it doesnt like a variable as a filename. If I put the filename like this
Const Filename = ("A_Tall_Tree")
it works. But a variable doesn't. How to work around this??



Thanks,
Penguin
Please keep in mind that I am using classic ASP with MySQL database.

"It's nice to be important... but it's more important to be nice.
 
It's weird that I cannot get help on this.

Thanks,
Penguin
Please keep in mind that I am using classic ASP with MySQL database.

"It's nice to be important... but it's more important to be nice.
 
You can't assign a variable to a constant. Just use:
Code:
VID = REQUEST.FORM("TITLE_")
Dim VTX
VTX = VID&".TXT"
'RESPONSE.WRITE(VTX)
[highlight #FCE94F]Dim Filename 
Filename = VTX[/highlight]

Or better
Code:
VID = REQUEST.FORM("TITLE_")
Dim Filename 
Filename = VID&".TXT"
'RESPONSE.WRITE(Filename)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top