Facebook Twitter Reddit LinkedIn

Dynamically Including Files in ASP

There are times when you know that you'll want to include external ASP code within another ASP file, but you won't know which include file to use until run time. In this case, the usual #include directive won't help you a bit. Because of the way ASP script is processed before being sent to the client, you simply can't use a variable in an #include statement. But here's a sneaky workaround: use the FileSystemObject to read the include file's content and flush it out to the browser yourself, like this:

<%
strFile = request.queryString("file")

If strFile <> "" then
set fso = createObject("Scripting.FileSystemObject")
set fsoFile = fso.openTextFile(strFile)
' <pre> preserves the original format of the include file

response.write "<pre>" & fsoFile.readAll & "</pre><br/>"

set fso = nothing
set fsoFile = nothing

end if
%>

Source: Jason Fisher
© 2007-2018 All rights reserved.   Part of the somuch.com group of trusted web sites.   PO Box 1542, Ormond Beach FL 32175