Link to home
Start Free TrialLog in
Avatar of kevp75
kevp75Flag for United States of America

asked on

File Include Loop?

I'm wondering if there is a way to loop through the files in a folder and include each file.  For example

say I have /includes, and in it are db.inc, print.inc, email.inc

I want to be able to do something along these lines

For each file in folder
  <!--#include virtual='/includes/"&file.Name&"'-->
Next

is it possible?
ASKER CERTIFIED SOLUTION
Avatar of inviser
inviser

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevp75

ASKER

not sure how much more detailed I can be.

I have in my /webroot a folder called /includes, in it are all my include files, they contain all the functions I use throughout my site.

all I'm wondering is if there is a way to loop through the content in /includes to see if any files exist, and if they do include them.

for example:

set fso = createobject("scripting.filesystemobject")
  set fol = fso.GetFolder(server.mappath("/includes/")
    set files = fol.Files
      For each File in files
        response.write("<!--#include virtual=""/includes/" & File.Name & """-->") 'what about using server.execute?
      Next
    set files = nothing
  set fol = nothing
set fso = nothing
Avatar of inviser
inviser

You will need to explicity include each file. I would suggest making a master includes file in /webroot/includes that includes all the files in the folder. You will then need to include that master include file on every page that needs it within your website.

Although this may seem like a pain, it is a commonly accepted practice and a much better idea than trying to create a function to loop through the folder and include all the files.
Again, remember, "<!--#include virtual=""/includes/" & File.Name & """-->" will not work because include statements are run before the page is even executed. So in the statement above, when the include is run, the value of File.Name will not be know until later. All include statements must be with literal paths, no variables.
Avatar of kevp75

ASKER

that's what I've already done.  I've gone and seperated out the area specific functions into their own include files, and then included them in a main include file (which is included on every page)

I would like to simplify this, as I may need to add more include files, (in my laziness) I don't want to have to go and add in <!--#include...blah blah blah, everytime I add a file to that folder.

what's the difference between using file/virtual include over server.execute?
>> I would like to simplify this, as I may need to add more include files, (in my laziness)

I understand how you feel, but you should stick to manually adding the files each time. Any clever workaround would likely be a security breech or very inefficient.

file/virtual include is a preprocessor statement that simple pieces asp pages together into one before the execution of the asp pages as a whole

server.execute is a runtime command that will execute another asp script independently of the page that called the execution. Any output caused by the server.execute command will be included in the calling page's output, but no communication in terms of ASP code takes place and each execution is strictly independent of the other.
Avatar of kevp75

ASKER

thanks inviser