VB Script Primer
VB Script Primer
Michael Adams
3-10-2003: Version 1.0
3-11-2004: Began "examples" section
Website: www.wolfsheep.com
Intro
This quick guide is meant to provide a "starting point" for writing useful
scripts. For further solutions, you may wish to consider the following: at last
check, I'm not getting anything to endorse them, but these resources have
been EXTREMELY helpful...
Basics
1. If you know any BASIC or Visual BASIC, you're in the right territory.
The scripting system is more or less a runtime version of BASIC without
the full functionality of those languages, but far more useful than batch
files & a good way to start programming (the ONLY advantages to regular
BASIC are graphics support & the ability to build EXEs).
2. VBScript files are written using .VBS files editable in any text editor.
The files are executed using cscript.exe for Command-line output, and
wscript.exe for Windows/GUI applications. Support is built into most
versions of Windows beyond Windows 95: the latest functions are
built-into Windows 2000 & XP; Internet Explorer 5 & up provides updated
support for non-2000/XP systems.
Dim strExample,intExample
strExample = "This is a string!"
intExample = 1000
5. GUI input & output are largely handled by "InputBox" & "MsgBox."
Dim string
string=InputBox("I hate ","Woohoo")
If string <> "" Then MsgBox "I hate " + string,0,"Eeek"
Dim strRunMe
strRunMe = "notepad"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run"C:\Windows\"+strRunMe+".exe"
Examples
1. This script I threw together to make a .NET program run off a network share.
It was very hard to find results online, and even this may only work for non-domain
shares: I think some domain permissions still keep this from working right.
'Variables
Dim WshShell, proc,objFileSystem,strWinRootFldr
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set strWinRootFldr = objFileSystem.GetSpecialFolder(WindowsFolder)
Set WshShell = WScript.CreateObject("WScript.Shell")
' -url entry is the application folder or files: permissions are relaxed on that folder before
program execution
Set proc = WshShell.Exec(strWinRootFldr&"\Microsoft.NET\Framework\v1.1.4322\caspol
-machine -addgroup PriceList -url file://mpadamscpu/temp/* FullTrust -polchgprompt off")
' Wait for application to exit
Do While proc.Status = 0
WScript.Sleep 1
Loop
Tables
Code Result
0 "Ok"
1 "Ok" & Cancel"
2 "Abort","Retry","Cancel"
3 "Yes","No","Cancel"
4 "Yes","No"
5 "Abort","Cancel"