View Full Version : Executing Code from text file

01-19-2005, 05:58 PM
I'm reading from a textfile but I want to know how to execute ASP code stored in a text file. Is it possible?

DIM Page, FSO, TSO,RL, Temp1, Temp2, Temp3
Dim Cont as String, URL1
Page = REQUEST("Page")
URL1 = Server.MapPath("Development/" & Page)
FSO = Server.CreateObject("Scripting.FileSystemObject")
if NOT FSO.Fileexists(URL1) then
URL1 = Server.MapPath("Articles/Article1/Article1") & ".txt"
Page = "Article1"
end if
TSO = FSO.OpenTextFile(URL1)
RL = TSO.ReadLine
If instr(RL, "Menu Title: ") then
temp1 = Replace(RL,"Menu Title: ", "")
else temp1 = "Page Links:"
end if
<div class="layout_leftcontainer">
<div id="vnav1">
<div class="vnav1_top"></div><ul>
<li class="vnav1_title"><%Response.write(Temp1)%></li>

<li><a href="/Articles/" target="_self">Go Back</a></li>
Do While Not TSO.AtEndOfStream
RL = TSO.ReadLine
If RL = "Start Link" then
RL = ""
temp1= Replace(TSO.ReadLine, "LinkName: ", "")
temp2= Replace(TSO.ReadLine, "LinkURL: ", "")
temp3= Replace(TSO.ReadLine, "LinkTarget: ", "")%>
<li><a href="<%=temp2%>" target="<%=temp3%>"><%=temp1%></a></li>
end if
Cont = Cont & RL

Cont = Replace(Cont, "[Path]","Articles/" & Page & "/")

01-19-2005, 09:09 PM
adovbs.inc and cdovbs.inc are two such examples

The thing is if your code is in a text file then it can be read by a user. Security wise it is not a good idea.

01-20-2005, 04:31 AM
I hate to include files and if you include the file in a text file, it will not work because the include files are processed before actual runtime.

There must be a function that lets you execute code from a text file?

01-21-2005, 02:37 PM
Ever heard of functions or sub routines? Why would you want to include code in a text file, bad idea. Look into functions.

01-21-2005, 03:55 PM
Create your file as an *.asp file, NOT as a text file. Include the asp file.

Within that file you can have functions you can call from the including page....


01-21-2005, 09:29 PM
If I creeate it as a ASP file instead of text file and include THAT file, theres going to be a 1000 includes LOL.

I just want to execute a piece of asp code from text file. Is it possible or not? thats what i want to know

01-21-2005, 09:50 PM
Nope - not possible.

Why do you say that there will be 1000 includes? Makes no sense to me...


01-21-2005, 10:31 PM
Yes you can, HOWEVER it will run at the very beginning of the page, no matter where you place the include file on the page. Why not just save the code snippet as .asp? This will also save the code from prying eyes.

Oh and if you don't believe me do something simple like this

Dim TodaysDate
TodaysDate = Date()
Response.write. TodaysDate

Save that as test.txt

then on an asp page add this code
<!--#include file="test.txt"-->
save the page on your server and run it . No matter where you place the include file on your page, the date will show at the very top of the page.

change the name to test.asp and where ever you place the include file, that is where the date will be displayed.

01-22-2005, 03:45 AM
That does not work.

Heres what Iam trying to do.

I have a bunch of articles stored in text files about computers.
Most of the articles are pure text but some of the articles need to connect to a database and do some stuff etc.

And all the articles are being read from a ASP page that reads text files etc.

01-22-2005, 09:29 PM
did you include the delimiters? <% %> if the are not part of the text file, then no it will not work. As for reading info from a text file your best bet is to use the file system object to read it .

01-25-2005, 04:53 PM
i dont know if server.execute("...") works with .txt files, that can be used instead of <!--#include file="..." --> but with a dynamic filename if you want it :p (asp3+ only but you probably are using asp3)

i once wrote a system that used fso to run txt files, as i wanted users to be able to write txt files that had some functionality such as variables and if statements without knowing asp, .. this just used a lot of complicated code to pretty much run a fake language i invented with very few commands for simplicity (like i had If_admin_... that would only write "..." if the reader realises its an admin logged on, etc)

01-25-2005, 09:21 PM
i dont know if server.execute("...") works with .txt files, that can be used instead of <!--#include file="..." --> but with a dynamic filename if you want it :p (asp3+ only but you probably are using asp3)

That's what I'd suggest you try as well.