Like somuch.com on Facebook Share somuch.com on Facebook somuch.com on Twitter Reddit LinkedIn Pinterest
Newest Tips & Tricks...

Home :: Tips & Tricks :: Active Server Pages

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

Recommended Links...