lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
VBS: vbDataset
One of the first things that struck me when I first started working with vbscript was, "Wow, arrays in vbscript are really crappy." I was ready to go on a whiny little tirade when I caught myself and said, "Well, maybe I'm just being spoiled after working with Php and Javascript. I'm sure in the old days, C programmers had to build their arrays with raw bits using their bare hands." I soon learned, however, that I'm not alone in lamenting vbscript array support:

VBScript Weaknesses: Unforgivably Bad Array Support

Still, I've tried to resist the temptation to bitch and moan and, instead, just make it work. Since vbscript does support classes, I figured I could whip up my own list-like data structure. While it has not been a slam dunk, I think I've come upon a decent solution that isn't outrageously convoluted. I call it vbDataset.

It's tailored to the project I'm working on at the moment, but may have more general applications. I've added it to my google code repository. If interested, you can find it there:

svn repository: vbDataset

It includes a test script.

Labels:

VBS: vbPrintR
Another vbscript function: vbPrintR is a function that will recursively print a vbscript variable for you. Similar to PHP's print_r function. Looked around the web for a pre-existing solution but couldnt find one, so I rolled my own. Find it in the klenwell code repository:

http://klenwell.googlecode.com/svn/trunk/VBS/vbprintr/

Includes a test script. On Windows, just right-click and open with Microsoft Window-Based Script Host. On anything else, why would you even care?

Labels:

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: