lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
VBS: Autoload External Files
Working in vbscript for the first time in a while and one of the first things to do was to write a script to autoload external files. This seems to work:


' *** FILE INCLUDE HANDLER
' Set Files to Include Here
dim aINCLUDE(2)
aINCLUDE(1) = "file1.inc.vbs"
aINCLUDE(2) = "file2.inc.vbs"

' Show Errors?
Const SHOW_ERRORS = True
Const HTML_BR = "
"

' *** SCRIPT ERROR HANDLER
' Config
dim oDictErr, iErrCount
Set oDictErr = CreateObject("Scripting.Dictionary")
iErrCount = 0

' Klenwell Error Trigger
Function kwTriggerError(sMessage)
iErrCount = iErrCount + 1
oDictErr.Add iErrCount, sMessage
If SHOW_ERRORS Then: MsgBox("Error #" & iErrCount & " : " & sMessage)
Err.Clear()
End Function

' Klenwell Error Dump
Sub kwDumpErrors
dim sOut, vKey
sOut = "Error Dump ("&iErrCount&" Errors): " & VbCrLf
For Each vKey In oDictErr
sOut = sOut & "Error " & vKey & " in oDictErr: " & oDictErr.Item(vKey) & VbCrLf
Next
MsgBox(sOut)
End Sub
' *** END SCRIPT ERROR HANDLER


' *** FILE INCLUDE HANDLER
' include file
Function kwIncludeFile(sFilePath)
On Error Resume Next
dim oFsoInc, oFileHandler, sIncContent
Set oFsoInc = CreateObject ("Scripting.FileSystemObject")

If sFilePath = "" Then: Exit Function: End If

If NOT oFsoInc.FileExists(sFilePath) Then
kwTriggerError("unable to load " & sFilePath & " : file not found")
Exit Function
End If

Set oFileHandler = oFsoInc.OpenTextFile(sFilePath,1)
If Err.number <> 0 Then
kwTriggerError("file " & sFilePath & " could not be opened")
Exit Function
End If

sIncContent = oFileHandler.ReadAll
oFileHandler.close
ExecuteGlobal sIncContent

Set oFsoInc = Nothing
Set oFileHandler = Nothing

End Function

' include all files
Sub kwIncludeFiles
dim sFile
For Each sFile In aINCLUDE
kwIncludeFile(sFile)
Next
End Sub

Call kwIncludeFiles


Includes error handling.

Labels:

Hi grreat reading your post