ExecuteGlobal ValidVBSCode 是在 W/CScript 托管脚本中重用/导入/包含 VBScript 代码 的简单方法。 ASP代码的问题是“”,所以你必须预处理.Readall()加载的代码。
当您提到“.ASP 文件中的数据”时,另一种方法可能是将文件 .ReadAll() 作为文本并解析 将信息转换为合适的(可能是复杂的)变量。
如果您需要更多帮助,请发布一个小而有代表性的文件样本以“包含”。
演示脚本(针对给定的示例):
Option Explicit
Dim sASP : sASP = Join(Array( _
" <% If MyVar = ""1"" Then" _
, " Data1 = ""this""" _
, " Data2 = ""that""" _
, " Else" _
, " Data1 = ""hi""" _
, " Data2 = ""there""" _
, " End If %>" _
), vbCrLf)
WScript.Echo sASP
Dim sExpr : sExpr = Replace(Replace(sASP, "<%", ""), "%>", "")
WScript.Echo sExpr
Dim MyVar, Data1, Data2
For Each MyVar In Split("1 2")
ExecuteGlobal sExpr
WScript.Echo myVar, Data1, Data2
Next
输出:
cscript 22821687.vbs
<% If MyVar = "1" Then
Data1 = "this"
Data2 = "that"
Else
Data1 = "hi"
Data2 = "there"
End If %>
If MyVar = "1" Then
Data1 = "this"
Data2 = "that"
Else
Data1 = "hi"
Data2 = "there"
End If
1 this that
2 hi there
更新 wrt 到 cmets 和 @Dennis 的演示代码:
我错误地假设(基于 w/cscript 拒绝它们)“”会导致问题。正如 Dennis 的代码所展示的(以及我的“阅读后”测试证实), Execute(Global) 可以很好地处理它们;不需要替换。 (所以我认为丹尼斯的回答值得称赞。)