VBScript Primer & Examples
History
- 3-10-2003: version 1.0.
- 3-11-2004: began "examples" section.
- 10-3-2007: converted to wiki.
Intro
This quick guide is meant to provide a "starting point" for writing useful scripts. My original references appear to have disappeared in the last few years, so I encourage you to search online for other references beyond the scope of this one.
Basics
Example code will be in typeset.
- 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).
- 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.
- Variables are declared using Dim statements. There is some variable typing supported, but I have not used it much yet & it is beyond the scope of this document. If it's string, integer, or window object: a simple "Dim" will work.
Dim strExample,intExample
strExample = "This is a string!"
intExample = 1000
- Looping: If-Then-Else; Select-Case; Do-While; Do-Until; and While-Wend are all supported.
Set WshShell = WScript.CreateObject("WScript.Shell")
If (MsgBox("Do you want display the system folders?",_
vbYesNo + vbQuestion, "Question") = vbYes) Then
WshShell.Run"C:\Windows"
WshShell.Run"C:\Windows\System32"
End If
- 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"
- External commands can be built up into an string executed by the "WshShell.Run" method provided by "WScript.Shell".
Dim strRunMe
strRunMe = "notepad"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run"C:\Windows\"+strRunMe+".exe"
- Files & folders can be copied, created, or destroyed with the "Scripting.FileSystemObject".
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
fso.createfolder "C:\Test"
fso.copyfile "C:\*.txt","C:\Test"
fso.deletefile "C:\Test\*.txt"
fso.deletefolder "C:\Test"
- Simple error checking is possible with the "Err" object & "On Error" events: "On Error Resume Next" will run the program "over" errors & "On Error Goto 0" will ignore errors completely.
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
fso.deletefolder "C:\Test"
If Err.Number <> 0 Then MsgBox "Folder does not exist",0,"Error"
Err.Clear 'clear error object
fso.createfolder "C:\Test"
fso.copyfile "C:\*.txt","C:\Test"
fso.deletefile "C:\Test\*.txt"
fso.deletefolder "C:\Test"
If Err.Number = 0 Then
MsgBox "Done",0,"Completed"
Else
MsgBox Err.Description,0,"Problems"
End If
Examples
- 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
'This is where you run the .NET program
Set proc = WshShell.Exec("Pricebook.exe")
' Wait for application to exit
Do While proc.Status = 0
WScript.Sleep 1
Loop
WshShell.Exec(strWinRootFldr&"\Microsoft.NET\Framework\v1.1.4322\caspol
-remgroup PriceList")
Tables
Methods covered in this document
|
Method
|
Format
|
Returns
|
Use
|
|
MsgBox
|
window text, code, window title
|
|
Display a message box with variables or text.
|
|
InputBox
|
window text, window title
|
User-entered value
|
Get user data to put into a variable.
|
|
WScript.Shell.Run
|
command string
|
|
Run a command line; open a file window; etc.
|
|
WScript.Shell.Exec
|
command string
|
|
I think this does a "run" & returns a value when done.
|
|
Scripting.FileSystemObject.CreateFolder
|
string of folder name
|
|
Create a folder
|
|
Scripting.FileSystemObject.DeleteFolder
|
string of folder name
|
|
Delete a folder
|
|
Scripting.FileSystemObject.CopyFile
|
string of file name,string of destination
|
|
Copy a file, or multiple with wildcards (i.e. *.txt)
|
|
Scripting.FileSystemObject.DeleteFile
|
string of file name
|
|
Delete a file, or multiple with wildcards (i.e. *.txt)
|
|
Err.Number
|
|
Whole number
|
Number of last error (0 = no error)
|
|
Err.Description
|
|
String
|
Windows' description of last error
|
MsgBox codes
|
Code
|
Result
|
|
0
|
"Ok"
|
|
1
|
"Ok" & Cancel"
|
|
2
|
"Abort","Retry","Cancel"
|
|
3
|
"Yes","No","Cancel"
|
|
4
|
"Yes","No"
|
|
5
|
"Abort","Cancel"
|