VB Script Objects

VB Script Objects 
a) FileSystemObject


Scripting allows us to process drives, folders, and files using the FileSystemObject (FSO) object model.

Creating FileSystemObject:

Set Variable=CreateObject("Scripting.FileSystemObject")
Example:

Dim objFso
Set objFso=CreateObject("Scripting.FileSystemObject")
objFso.CteateTextFile("D:\gcreddy.txt")

b) Dictionary


Creating Dictionary Object:

Set Variable=CreateObject("Scripting.Dictionary")
Example:

c) Excel Application



Creating Excel Object:

Set Variable=CreateObject("Excel.Application")
Example:

d) Word Application



Creating Word Object:

Set Variable=CreateObject("Word.Application")
Example:

e) Shell



Creating Shell Object:

Set Variable= WScript.CreateObject("Wscript.Shell")
Example:

f) Network



Creating Network Object:

Set Variable= WScript.CreateObject("WScript.Network")
Example:

g) PowerPoint



Creating PowerPointObject:

Set Variable=CreateObject("PowerPoint.Application")
Example:

h) ADODB Connection



The ADO Connection Object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database.


Creating Database Connection Object:

Set Variable=CreateObject("ADODB.Connection")
Example:

i) ADODB RecordSet


The ADO Recordset object is used to hold a set of records from a database table. A Recordset object consist of records and columns (fields).


Creating Database RecordSet Object:

Set Variable=CreateObject("ADODB.RecordSet")
Example:

j) ADODB Command


The ADO Command object is used to execute a single query against a database. The query can perform actions like creating, adding, retrieving, deleting or updating records.


Creating Database Command Object:

Set Variable=CreateObject("ADODB.Command")
Example:

k) Error 



Creating Error Object:

l) RegExp 


 Creating RegExp Object:


Set objReg=CreateObject("vbscript.regexp")

m) Internet Explorer




n) Outlook Express


 
a) Dictionary Object

 Dictionary Object that stores data key, item pairs.

A Dictionary object is the equivalent of a PERL associative array/Hash Variable. Items can be any form of data, and are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.




Creating a Dictionary Object:

Set objDictionary = CreateObject("Scripting.Dictionary") 
Dictionary Objects Methods: 


Add Method 

Adds a key and item pair to a Dictionary object

Exists Method

Returns true if a specified key exists in the Dictionary object, false if it does not.

Items Method

Returns an array containing all the items in a Dictionary object.

Keys Method

Returns an array containing all existing keys in a Dictionary object.

Remove Method

Removes a key, item pair from a Dictionary object.

RemoveAll Method

The RemoveAll method removes all key, item pairs from a Dictionary object.
 

Example:


Dim cities
Set cities = CreateObject("Scripting.Dictionary")
cities.Add "h", "Hyderabad"
cities.Add "b", "Bangalore"
cities.Add "c", "Chennai"



Dictionary Objects Properties:
Count Property

Returns the number of items in a collection or Dictionary object. Read-only.

CompareMode Property
Sets and returns the comparison mode for comparing string keys in a Dictionary object.

Key Property

Sets a key in a Dictionary object.

Item Property
Sets or returns an item for a specified key in a Dictionary object. For collections, returns an item based on the specified key. Read/write.

 

Examples:


1) Add Elements to a Dictionary


Set
objDictionary=CreateObject("Scripting.Dictionary")

objDictionary.Add
"Printer1","Printing"   
objDictionary.Add
"Printer2","Offline"
objDictionary.Add
"Printer3","Printing"

2) Delete All Elements from a Dictionary

Set
objDictionary=CreateObject("Scripting.Dictionary")


objDictionary.Add
"Printer1","Printing"   
objDictionary.Add
"Printer2","Offline"
objDictionary.Add
"Printer3","Printing"
colKeys
=objDictionary.Keys

Wscript.Echo
"Firstrun:"
For
EachstrKeyincolKeys
    Wscript.EchostrKey
Next

objDictionary.RemoveAll
colKeys
=objDictionary.Keys

Wscript.Echo
VbCrLf&"Secondrun:"
For
EachstrKeyincolKeys
    Wscript.EchostrKey
Next

3) Delete One Element from a Dictionary

Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "Printer 1", "Printing"  
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"

colKeys = objDictionary.Keys

Wscript.Echo "First run: "
For Each strKey in colKeys
    Wscript.Echo strKey
Next

objDictionary.Remove("Printer 2")
colKeys = objDictionary.Keys

Wscript.Echo VbCrLf & "Second run: "
For Each strKey in colKeys
    Wscript.Echo strKey
Next

4) List the Number of Items in a Dictionary


Set
objDictionary=CreateObject("Scripting.Dictionary")

objDictionary.Add
"Printer1","Printing"   
objDictionary.Add
"Printer2","Offline"
objDictionary.Add
"Printer3","Printing"
Wscript.Echo
objDictionary.Count


5) Verify the Existence of a Dictionary Key

Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "Printer 1", "Printing"  
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"

If objDictionary.Exists("Printer 4") Then
    Wscript.Echo "Printer 4 is in the Dictionary."
Else
    Wscript.Echo "Printer 4 is not in the Dictionary."
End If

0 comments:

Post a Comment