ASP itself does not support dynamic inclusion of files. Current dynamic inclusion uses FSO to merge the included files into the main file and then run it. In the following, the ordinary include file method in the form of <!--#include file="filename.asp" --> is also called "traditional reference", and the dynamic include file implemented by functions is called "dynamic reference". Common programs are as follows:
Function include(filename)
Dim re,content,fso,f,aspStart,aspEnd
set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
set f=nothing
set fso=nothing
set re=new RegExp
re.pattern="^s*="
aspEnd=1
aspStart=inStr(aspEnd,content,"<%")+2
do while aspStart>aspEnd+1
Response.write Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content,"%>")+2
Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write "))
aspStart=inStr(aspEnd,content,"<%")+2
loop
Response.write Mid(content,aspEnd)
set re=nothing
End Function
usage example: include("youinc.asp")
However, this function does not work when there are included files in the included files. Based on the above function, I improved the following function. In the included file, there is also a normal include file <!--#include file="filename.asp" --> which can also run normally.
Function includeconvert(oRegExp, strFilename, strBlock)
Dim incStart, incEnd, match, oMatches, str, code
'Extract the file name of the include part in the same way as the ASP code, and output the rest as it is
code = ""
incEnd = 1
incStart = InStr(incEnd,strBlock,"<!--#include ") + 13 'To find a target string <!--#include is exactly 13 characters, so +13
Do While incStart>incEnd+12 'The minimum distance between two references is continuous--><--#, incStart is 13 characters counting from <!--#include, so it must be at least 13- more than the previous incEnd 1 The obtained condition of >incEnd+12
str = Mid(strBlock,incEnd,incStart-incEnd-13)
str = Replace(str, """", """""") 'Replace a single double quote with two double quotes
str = Replace(str, VbCr, "")
str = Replace(str, VbLf, "")
str = Replace(str, VbCrLf, "")
code = code & VbCrLf & "Response.Write """ & str & """"
incEnd=InStr(incStart,strBlock,"-->")+3
oRegExp.pattern="(w+)=""([^""]+)""" 'Match file="filename.ext" or virtual="virtualname.ext", capture the two substrings of type and file name
Set oMatches = oRegExp.Execute(Mid(strBlock,incStart,incEnd-incStart-3))
Set match = oMatches(0) 'When it is determined that there is only one set of captures, to get the matching substrings of this set, you can do this without using For Each match In oMatches... Next
code = code & include(Mid(strFilename, 1, InStrRev(strFilename, "/")) & match.SubMatches(1)) 'Mid(filename, 1, InStrRev(filename, "/")) is being referenced When the sub-file name has a path, extract the path and add it in front of the file name traditionally referenced in the sub-file to find the correct path to open the file, because the file path in dynamic reference is relative to the main file. To match the second substring, use SubMatches(1)
incStart = InStr(incEnd,strBlock,"<!--#include ")+13
Loop
str = Mid(strBlock,incEnd)
str = Replace(str, """", """""") 'Replace a single double quote with two double quotes
str = Replace(str, VbCr, "")
str = Replace(str, VbLf, "")
str = Replace(str, VbCrLf, "")
code = code & VbCrLf & "Response.Write """ & str & """"
includeconvert = code
End Function
Function include(filename)
Dim re, content, fso, f, aspStart, aspEnd, code
Set fso=CreateObject("scripting.FileSystemObject")
Set f=fso.OpenTextFile(Server.MapPath(filename))
content=f.ReadAll
f.close
Set f=nothing
Set fso=nothing
code = ""
aspEnd=1
aspStart=InStr(aspEnd,content,"<%")+2
Set re=new RegExp
Do While aspStart>aspEnd+1
'Traditional reference <!--#inclde must be outside the ASP code segment, so transfer it first.
code = code & includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2))
aspEnd=InStr(aspStart,content,"%>")+2
re.pattern="^s*=" 'This regular replacement originally replaced <% = str %> with the standard <%Response.Write str %>
code = code & VbCrLf & re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ") 'Add carriage return and line feed before the ASP block to avoid multiple Response.Write between connecting blocks errors on the same line
aspStart=InStr(aspEnd,content,"<%")+2
Loop
code = code & includeconvert (re, filename, Mid(content,aspEnd))
Set re=nothing
include = code
End Function
For convenience, the above function ultimately returns the entire ASP code that integrates the included file. When using it, you need to use Execute to execute it, that is, when using it, you need: Execute(include("file.asp")).
The above function passes the test when the path of the included file and the main file are the same. It does not make further fault tolerance when the paths of the included file and the main file are different. The time is limited. Interested friends are welcome to provide comments and improvements.