six demon bag
Wind, fire, all that kind of thing!
2013-08-27
Import other script files in VBScript
The VBScript language doesn't provide a feature for including other code files, so you can't easily build and import code libraries. However, the missing feature can be emulated using the ExecuteGlobal
statement in a custom Import
procedure.
'! Import the first occurrence of the given filename from the working directory
'! or any directory in the %PATH%.
'!
'! @param filename Name of the file to import.
'!
'! @see http://gazeek.com/coding/importing-vbs-files-in-your-vbscript-project/
Private Sub Import(ByVal filename)
Dim fso, sh, code, dir
' Create my own objects, so the procedure is self-contained and can be called
' before anything else in the script.
Set fso = CreateObject("Scripting.FileSystemObject")
Set sh = CreateObject("WScript.Shell")
filename = Trim(sh.ExpandEnvironmentStrings(filename))
If Not (Left(filename, 2) = "\\" Or Mid(filename, 2, 2) = ":\") Then
' filename is not absolute
If Not fso.FileExists(fso.GetAbsolutePathName(filename)) Then
' file doesn't exist in the working directory => iterate over the
' directories in the %PATH% and take the first occurrence
' if no occurrence is found => use filename as-is, which will result
' in an error when trying to open the file
For Each dir In Split(sh.ExpandEnvironmentStrings("%PATH%"), ";")
If fso.FileExists(fso.BuildPath(dir, filename)) Then
filename = fso.BuildPath(dir, filename)
Exit For
End If
Next
End If
filename = fso.GetAbsolutePathName(filename)
End If
code = fso.OpenTextFile(filename).ReadAll
ExecuteGlobal code
Set fso = Nothing
Set sh = Nothing
End Sub
I found this on http://gazeek.com/coding/importing-vbs-files-in-your-vbscript-project/, but since the domain seems to be offline I'm documenting it here.
Posted 14:00 [permalink]