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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic include file from folder content

Status
Not open for further replies.

KarlZ

IS-IT--Management
Mar 4, 2003
5
SE
Hi,

I'm trying to create a page that reads files from a folder and includes them in the page itself. Not just the name of the file, but the content as is.

I'm not very good at this coding-thing, (if at first you don't succeed...) smile
I had to split the "include file"-codebit to make it work as far as this.

Tthe sourse-code of the page looks just the way it should, but the included page won't render.

I hope you understand what I'm getting at.

What I want is this:
Name of file printed <br>
File content showing (the file is a bit of code that may be both images and text)<br>

My research has lead me to understand that this approach might not be the best one since the "include file" must be processed earlier to show up.

Any suggestions, anyone?
help much appreciated!

/K

My code looks like this so far...

<%
Dim fso, fld, fil
Dim incl1Text, incl2Text, inclEndText
incl1="<!--#incl"
incl2="ude file="
inclEnd="-->"
Dim inclComplete
inclComplete=incl1 & incl2
Dim incl
Dim strBody

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(Server.MapPath("../files"))
For Each fil in fld.Files
incl = inclComplete & """../files/" & fil.Name & """" & inclEnd

strBody = strBody & fil.Name & "<br>" & incl & "<br>"
Next
Response.Write(strBody)

%>
 
server side includes are pre-processed before the ASP parser is executed, so using <!--#include..... etc will mean that file will be added into the raw script before anything is 'run'. Dynamically building that directive simply bypasses the pre-processor that brings in the file, so nothing will come through. So, either way, it wont work.

The way to dynamically include content from files is to use the FSO that you're already using, to read the contents and (if it contains ASP scripting) use Execute(sTheContentOfYourFile) to process. If it only includes html, js, css, etc, (i.e. client side stuff) then just write out the raw content, as this is rendered by the browser, not the ASP server.

e.g.: or


A smile is worth a thousand kind words. So smile, it's easy! :)
 
When damber wrote "If it only includes html, js, css, etc, (i.e. client side stuff) then just write out the raw content, as this is rendered by the browser, not the ASP server." he meant to use the FSO's OpenTextFile function to get a TextStream object and us its ReadAll function to grab the insides of the file and Response.Write them to browser.
 

(see second link above)

A smile is worth a thousand kind words. So smile, it's easy! :)
 

Also, you will notice a difference in variable scoping and availability in the Execute vs Server.Execute methods - Server.Execute acts less like a normal include than parsing and Execute()-ing as it doesn't share page-scope variables. This is why for scenarios trying to duplicate the SSI feature, but with dynamic file selection, the Execute method is nearer to the desired behaviour.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Yeah, what Damber said.

(Sorry D, I scanned through your post too quickly.)
 
Hi, hope I can get help from here:

what's the right tool or way to go to publish product catalog information from ERP sql database to the website?

assume users can only see the information on website but not editing or changing.


Frances
 
Frances, best to start a new thread. You'll get better responses.
 

(Sorry D, I scanned through your post too quickly.)

No worries, I wrote the post too quickly so didn't explain things particularly clearly ;-)

It depends on what the OP wants to do - Server.Execute() may be the path with least work if it's just simple self contained code.

Maybe one day the OP will come back and enlighten us :)



=======================================
LessThanDot - The IT Community of the 21st Century

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top