![]() |
||||||
| Home | Dev | Web | Blog | |||
![]() |
|
Providing configuration files for your VBScripts
Recently I was writting a bunch of scripts in vbscript to
automate some of my daily tasks of file management and system monitoring (snapshots
of parts of registry to detect any malicious software for instance). After writing
several scripts I found myself needing something like .NET config files for scripts.
For instance you are writing a script that reads some files in directory, parses their contents and writes log file or some output files to another place in your network/drive. Now you want to make the source directory and destination directory variables easily configurable so that you can reuse it for various conditions without actually having to modify the script file. To do that, I came up with a config file reader which initializes a property bag for your script. Your main scripts access the property bag using keys specified in the config file and gets that value that is associated with that key. My idea of config file is very simple. For instance here is one:
# SAMPLE CONFIG FILE The config files allows comments and has an easy to use syntax. Every part must be seperated by a semicolon for config file to work. Next thing that comes into play is the config file reader that will expose the property bag to the interested script:
public function GetGoodyBag(filename)
dim goodies
set goodies = CreateObject("Scripting.Dictionary")
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
dim file
set file = fso.OpenTextFile(filename & ".conf", 1)
dim content
content = file.ReadAll
'add vbCrLf incase peeps forgot to do that
content = content & vbCrLf
dim endP, startP, index, subStart, subEnd
dim entry, key, value
'strip comments and spaces
endP = instr(content, vbCrLf)
startP = 1
index = 0
subStart = 0
subEnd = 0
dim temp
temp = ""
While endP <> 0
entry = mid(content, startP, endP-startP)
if instr(entry, "#") = 0 and len(trim(entry)) <> 0 Then
temp = temp & entry & vbCrLf
end if
startP = endP + 2
endP = instr(endP+1, content, vbCrLf)
Wend
'no fill the goodie bag
content = temp
endP = instr(content, vbCrLf)
startP = 1
index = 0
subStart = 0
subEnd = 0
while endP <> 0
entry = mid(content, startP, endP-startP)
subStart = 0
subEnd = 0
'get key
subStart = InStr(entry, "=")
if (subStart <> 0) Then
subEnd = InStr(subStart, entry, ";")
if (subEnd <> 0) Then
key = trim(mid(entry, subStart+1, subEnd - subStart-1))
'get value
subStart = InStr(subEnd, entry, "=")
if (subStart <> 0) Then
subEnd = InStr(subStart, entry, ";")
if (subEnd <> 0) Then
value = trim(mid(entry, subStart+1, subEnd - subStart-1))
end if
end if
end if
end if
goodies.Add key, value
startP = endP + 2
endP = instr(endP+1, content, vbCrLf)
wend
set GetGoodyBag = goodies
End Function
First, to get the property bag from the script that is using the "ping.conf" config file for instance, do this:
dim goodies Simple! Finally, you don't want to include config file reading code inside your script. Instead, keep it in a seperate file, let's say config.vbs. Then, if you need to fix some bug or enhance log file reader, you just need to modify that one file. To hook up your script and config script, create a third script file with extension wsf (for windows script file) with this content:
<package> <job id="something"> <script language="vbscript" src="config.vbs"></script> <script language="vbscript" src="ping.vbs"></script> </job> </package> ping.vbs here is the file that contains your script and config.vbs is config file reader. To run your script, run "wscript file.wsf" where file.wsf has the above mentioned package xml. I hope this is useful for someone. Here are the files: config file, config reader, runner file. |
![]() |