Reputation: 728
I have a .VBS file that runs via scheduled task on my server. I have some data in a .ASP file that resides on my website that I want to pull in to that .VBS file so that I do not need to duplicate the same info in two places.
Here is a sample of the data in the ASP file:
<%
If MyVar = "1" Then
Data1 = "this"
Data2 = "that"
Else
Data1 = "hi"
Data2 = "there"
End If
%>
Is this possible? I think I recall something about ExecuteGlobal, but cannot put it all together - if that in fact can help here.
Upvotes: 3
Views: 1242
Reputation: 38745
ExecuteGlobal ValidVBSCode
is the easy way to re-use/import/include VBScript code into W/CScript hosted scripts. The problem with ASP code are the "<% ... %>", so you have to pre-process the code loaded by .Readall()
.
As you mention 'data in a .ASP file' an alternative approach may be to .ReadAll() the file as text and parse the info into a suitable (perhaps complex) variable.
If you need more help, publish a small but representative sample of the file to be 'included'.
Demo script (for the given sample):
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
output:
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
Update wrt to comments and @Dennis' demo code:
I wrongly assumed (based on w/cscript rejecting them) that "<%..%>" would cause problems. As Dennis' code demonstrates (and my 'after the reading' tests confirm), Execute(Global) handles them well; the replace(s) aren't necessary. (So I think Dennis' answer deserves credit.)
Upvotes: 4
Reputation: 728
Got this working! Here is a sample bit of code. THANKS for the help!
MyVar = "2"
Sub Import(ByVal strFile)
Set objFs = CreateObject("Scripting.FileSystemObject")
Set objFile = objFs.OpenTextFile(strFile)
strCode = objFile.ReadAll
objFile.Close
ExecuteGlobal(strCode)
End Sub
Import "E:\path\test.asp"
msgbox(Data1)
And the response is "hi".
Upvotes: 3