3

I was looking into dynamic "includes" methods and came down to a solution which uses VBScript's Execute function. This works perfectly for me but I noticed that Execute executes the code but this code cannot declare anything like a variable or function:

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(Server.MapPath(strFile)) Then
  Set objFile = objFSO.OpenTextFile(Server.MapPath(strFile), 1)
  strSource = objFile.ReadAll

  // Filter out comment- and ASP tags cos they return errors
  strSource = Replace(strSource, Chr(60)&Chr(37), "")
  strSource = Replace(strSource, Chr(37)&Chr(62), "")
  objRegExp.Pattern = "^[ \t]*(//¦\')[\s\S]*?$"
  strSource = objRegExp.Replace(strSource, "")

  // Execute the code
  On Error Resume Next
  Execute strSource 'etc........
end if

Why? Thank you!

Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
Vad
  • 3,658
  • 8
  • 46
  • 81

1 Answers1

2

Perhaps you want to use ExecuteGlobal instead. I imagine that your dynamic includes file loader is in a subroutine, so when you use Execute, the new variables are scoped within that subroutine. ExecuteGlobal will ensure that the new variables are available globally.

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40