Dynamic
The ASP itself does not support dynamic inclusion files, and the current dynamic inclusion is through the FSO to merge the included files into the main file and then run. The following are also the common inclusion files in the form <!--#include file= "filename.asp"-->, called "Traditional references", and dynamic inclusion files implemented by functions are called "Dynamic references". The common procedures 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 examples: include ("youinc.asp")
However, this function does not work when the file containing the complement contains files. On the basis of the above functions, I have improved the following functions, including the normal inclusion file <!--#include file= "filename.asp"--> also works.
Function Includeconvert (oregexp, strFileName, Strblock)
Dim Incstart, Incend, match, omatches, str, code
' Extracts the file name of the include part in the same way as the ASP code, and the remainder is output
code = ""
incend = 1
Incstart = InStr (Incend,strblock, "<!--#include") + 13 ' to find a target string <!--#include exactly 13 characters, so +13
do While incstart>incend+12 ' two reference spacing is a continuous--><--#,incstart is #include起数13个字符 from the <!--, so it takes at least more than the previous Incend 13-1 conditions of the obtained >incend+12
str = Mid (strblock,incend,incstart-incend-13)
str = replace (str, "" "" "" "" "") ' replaces single double quotes 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 type and filename two substrings
Set omatches = Oregexp.execute (Mid (strblock,incstart,incend-incstart-3))
Set match = omatches (0) ' to determine that only a set of captures, to get this set of matching substrings, you can do this, omit the for every match in omatches ... Next
Code = code & Include (Mid strFileName, 1, InStrRev (strFileName, "/") & match. Submatches (1)) ' Mid (filename, 1, InStrRev (filename, "/") extracts the path when the referenced child file name has a path, and adds it to the file name of the traditional reference in the child file to find the correct open file path. Because the file path for a dynamic reference is relative to the primary file. To a second matching substring with submatches (1)
Incstart = InStr (Incend,strblock, "<!--#include") +13
Loop
str = Mid (strblock,incend)
str = replace (str, "" "" "" "" "") ' replaces single double quotes 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 Quotes <!--#inclde must be outside the ASP code snippet, so go ahead.
Code = code & Includeconvert (Re, filename, Mid (content,aspend,aspstart-aspend-2))
aspend=instr (aspstart,content, "%\>") +2
re.pattern= "^\s*=" is replaced with <% = str% > to the standard <%response.write str% >
Code = code & VbCrLf & Re.replace (Mid (content,aspstart,aspend-aspstart-2), "Response.Write") ' ASP block before adding a carriage return line, To avoid multiple Response.Write errors in the same row between the connecting blocks
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 contains the file, and is executed with execute even when it is needed: Execute (Include ("file.asp")).
The above function tests the included file with the same path as the primary file, does not make further fault tolerance for the contained file and the main file path, and the time is limited, and welcome friends who are interested to make comments and improvements.