A few things to keep in mind when using the IncludeFile(p_Path)
method :
std_internal_LibFiles
before the first call to IncludeFile(p_Path)
IncludeFile(p_Path)
anywhere in your code, including other methods.So the main goal of this function is to :
' *************************************************************************************************
'! Includes a VbScript file
'! @param p_Path The path of the file to include
' *************************************************************************************************
Sub IncludeFile(p_Path)
' only loads the file once
If std_internal_LibFiles.Exists(p_Path) Then
Exit Sub
End If
' registers the file as loaded to avoid to load it multiple times
std_internal_LibFiles.Add p_Path, p_Path
Dim objFso, objFile, strFileContent, strErrorMessage
Set objFso = CreateObject("Scripting.FileSystemObject")
' opens the file for reading
On Error Resume Next
Set objFile = objFso.OpenTextFile(p_Path)
If Err.Number <> 0 Then
' saves the error before reseting it
strErrorMessage = Err.Description & " (" & Err.Source & " " & Err.Number & ")"
On Error Goto 0
Err.Raise -1, "ERR_OpenFile", "Cannot read '" & p_Path & "' : " & strErrorMessage
End If
' reads all the content of the file
strFileContent = objFile.ReadAll
If Err.Number <> 0 Then
' saves the error before reseting it
strErrorMessage = Err.Description & " (" & Err.Source & " " & Err.Number & ")"
On Error Goto 0
Err.Raise -1, "ERR_ReadFile", "Cannot read '" & p_Path & "' : " & strErrorMessage
End If
' this allows to run vbscript contained in a string
ExecuteGlobal strFileContent
If Err.Number <> 0 Then
' saves the error before reseting it
strErrorMessage = Err.Description & " (" & Err.Source & " " & Err.Number & ")"
On Error Goto 0
Err.Raise -1, "ERR_Include", "An error occurred while including '" & p_Path & "' : " & vbCrlf & strErrorMessage
End If
End Sub
To include a file in another file, just use the one liner :
IncludeFile "myOtherFile.vbs"
Before we use the IncludeFile method, we need to :
std_internal_LibFiles
globallyDim std_internal_LibFiles
Set std_internal_LibFiles = CreateObject("Scripting.Dictionary")