You are on page 1of 5

VBScript Primer & Examples

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...

"VBScript Programmer's Reference" (Wrox Press)


Windows Scripting Solutions
"Windows Scripting" Home (Microsoft Corporation)

Basics

Example code will be in italics.

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.

3. 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

4. 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

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"

6. 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"

7. 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"

8. 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

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

'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, Display a message box with variables or text.
code, window
title
InputBox window text, User- Get user data to put into a variable.
window title entered
value
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.Cr string of folder Create a folder
eateFolder name
Scripting.FileSystemObject.De string of folder Delete a folder
leteFolder name
Scripting.FileSystemObject.Co string of file Copy a file, or multiple with wildcards (i.e.
pyFile name,string of *.txt)
destination
Scripting.FileSystemObject.De string of file name Delete a file, or multiple with wildcards (i.e.
leteFile *.txt)
Err.Number Whole Number of last error (0 = no error)
number
Err.Description String Windows' description of last error
MsgBox codes (as in MsgBox "Message",code,"Title")

Code Result
0 "Ok"
1 "Ok" & Cancel"
2 "Abort","Retry","Cancel"
3 "Yes","No","Cancel"
4 "Yes","No"
5 "Abort","Cancel"

You might also like