Saturday, 5 September 2015

VBScript » Objects » Dictionary


VBScript » Objects » Dictionary

VBScript » Objects » Dictionary

Version: 2.0
The Dictionary object stores name/value pairs (referred to as the key and item respectively) in an array. The key is a unique identifier for the corresponding item and cannot be used for any other item in the same Dictionary object.

Examples

Code:
<% 
Dim cars 
Set cars = CreateObject("Scripting.Dictionary") 
cars.Add "a", "Alvis" 
cars.Add "b", "Buick" 
cars.Add "c", "Cadillac" 
Response.Write "The value corresponding to the key 'b' is " 
cars.Item("b") 
%> 
Output:
"The value corresponding to the key 'b' is Buick"
Explanation:
This code creates a Dictionary object called "cars", adds some key/item pairs, retrieves the item value for the key 'b' using the Item property and then outputs the resulting string to the browser.

Properties

CompareMode
Syntax: object.CompareMode[ = comparison_mode]
The CompareMode property is used to set and return the key's string comparison mode which determines how keys are matched while looking up or searching. Syntax: object. Count Property Syntax: object.
Count
Syntax: object.Count
The Count property is used to determine the number of key/item pairs in the Dictionary object.
Item
Syntax: object.(key) [ = itemvalue]
The Item property allows us to retreive the value of an item in the collection designated by the specified key argument and also to set that value by using itemvalue.
Key
Syntax: object.Key(keyvalue) = newkeyvalue
The Key property lets us change the key value of an existing key/item pair.

Methods

Add
Syntax: object.Add keyvalue, itemvalue
The Add method is used to add a new key/item pair to a Dictionary object.
Exists
Syntax: object.Exists (keyvalue)
The Exists method is used to determine whether a key already exists in the specified Dictionary. Returns True if it does and False otherwise.
Items
Syntax: [arrayname = ] object. Items
The Items method is used to retreive all of the items in a particular Dictionary object and store them in an array.
Keys
Syntax: object.Keys
The Keys method is used to retreive all of the keys in a particular Dictionary object and store them in an array.
Remove
Syntax: object.Remove(keyvalue)
The Remove method is used to remove a single key/item pair from the specified Dictionary object.
RemoveAll
Syntax: object.RemoveAll
The RemoveAll method is used to remove all the key/item pairs from the specified Dictionary object.

VBScript » Objects » RegExp


VBScript » Objects » RegExp

The RegExp object is used to look for and match all occurrences of a search string pattern inside a target string.

Each time the RegExp object finds a match during the search, a Match object is created and added to a Matches collection.

The search pattern is declared using the Pattern property. It could, for example, be a simple string, such as "cost analysis". However, the search pattern can also be a regular expression. Regular expressions can range from being very simple to being extremely complex. The Pattern property page contains an introductory listing of special characters that can be used with regular expressions.

Examples

Code:
<% 
'this sub finds the matches 
Sub RegExpTest(strMatchPattern, strPhrase) 
'create variables 
Dim objRegEx, Match, Matches, StrReturnStr 
'create instance of RegExp object 
Set objRegEx = New RegExp 
'find all matches 
objRegEx.Global = True 
'set case insensitive 
objRegEx.IgnoreCase = True 
'set the pattern 
objRegEx.Pattern = strMatchPattern 
'create the collection of matches 
Set Matches = objRegEx.Execute(strPhrase) 
'print out all matches 
For Each Match in Matches 
strReturnStr = "Match found at position " 
strReturnStr = strReturnStr Match.FirstIndex ". Match 
Value is '" 
strReturnStr = strReturnStr Match.value "'." 
'print 
Response.Write(strReturnStr "<BR>") 
Next 
End Sub 
'call the subroutine 
RegExpTest "is.", "Is1 is2 Is3 is4" 
%>
Output:
Match found at position 0. Match Value is 'Is1'. Match found at position 4. Match Value is 'is2'. Match found at position 8. Match Value is 'Is3'. Match found at position 12. Match Value is 'is4'.
Explanation:
The following code is a simpler, working version of a program published by Microsoft. Note the use of the keyword New when you create a RegExp object using Set.

Properties

Global
Syntax: object.Global = [ True | False ]
This property is only used with a RegExp object variable and returns a Boolean value. False signifies that a search should only find the first occurrence of a match. True signifies that a search should find all occurrences of a match.
IgnoreCase
Syntax: object.IgnoreCase
This property is only used with a RegExp object variable and returns a Boolean value. False signifies that a search should be case-sensitive (i.e., upper versus lower case). True signifies that a search should ignore case in a match.
Pattern
Syntax: object.Pattern
This property defines the regular expression or search pattern string that is to be matched during the search.

Methods

Execute
Syntax: object.Execute (TargetString)
This method is used to execute the search and to look for matches of the search pattern string (or regular expression) and the target string. Each time a match is made, a Match object is created and added to a collection that is called a Matches collection. Syntax: object.
Replace
Syntax: object.Replace (String1, String2)
This method is used to replace text found in a regular expression search. Do not confuse this method with the Replace function.
Test
Syntax: object.Test
This method is used to determine if the search pattern occurs within a specified string and returns a Boolean value to signify the results of the search. True is returned if the pattern is found. Otherwise, False is returned.

VBScript Objects in FileSystemObject


VBScript Objects in FileSystemObject

The FileSystemObject is used to gain access to a computer's file system. It can create new files and access existing ones.

Examples

Code:
<% 
dim filesys, filetxt, getname, path 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True) 
path = filesys.GetAbsolutePathName("c:\somefile.txt") 
getname = filesys.GetFileName(path) 
filetxt.WriteLine("Your text goes here.") 
filetxt.Close 
If filesys.FileExists(path) Then 
Response.Write ("Your file, '" getname "', has been created.") 
End If 
%>
Output:
"Your file, 'somefile.txt', has been created."
Explanation:
This code uses the CreateTextFile method of the FileSystemObject object to create a text file (c:\somefile.txt) and then writes some text to it.

Properties

Drives
Syntax: drvcollection = ] object.Drives
Returns a Drives collection consisting of all the Drive objects on a computer.

Methods

BuildPath
Syntax: [newfullpath = ]object.BuildPath(path, name)
This method is used to append a name onto an existing path.
CopyFile
Syntax: object.CopyFile source, destination [,overwrite]
This method allows us to copy one or more files from one location (the source) to another (destination).
CopyFolder
Syntax: object.CopyFolder source, destination [, overwrite]
Copies one or more folders and all contents, including files and subfolders, from one location to another.
CreateFolder
Syntax: object.CreateFolder (foldername)
This method allows us to create a folder with the specified foldername.
CreateTextFile
Syntax: object.CreateTextFile filename [,overwrite[, unicode]]
Creates a text file and returns a TextStreamObject that can then be used to write to and read from the file.
DeleteFile
Syntax: object.DeleteFile file [,force]
This method deletes a specified file or files (using wilcards).
DeleteFolder
Syntax: object.DeleteFolder folder [,force]
This method deletes a specified folder, including all files and subfolders.
DriveExists
Syntax: object.DriveExists(drive)
This method lets us check if a specified drive exists. It returns True if the drive does exist and False if it doesn't.
FileExists
Syntax: object.FileExists(file)
Lets us check whether a specified file exists. Returns True if the file does exist and False otherwise.
FolderExists
Syntax: object.FolderExists(folder)
Allows us to check if a specified folder exists. Returns True if the folder does exist and False if it doesn't.
GetAbsolutePathName
Syntax: object.GetAbsolutePathName(path)
This method gets the complete path from the root of the drive for the specified path string.
GetBaseName
Syntax: object.GetBaseName(path)
This method gets the base name of the file or folder in a specified path.
GetDrive
Syntax: object.GetDrive(drivename)
This method returns a Drive object corresponding to the drive in a supplied path.
GetDriveName
Syntax: object.GetDriveName(path)
This method gets a string containing the name of the drive in a supplied path.
GetExtensionName
Syntax: object.GetExtensionName(path)
Used to return a string containing the extension name of the last component in a supplied path.
GetFile
Syntax: object.GetFile (filename)
Returns the File object for the specified file name.
GetFileName
Syntax: object.GetFileName(path)
This method is used to return the name of the last file or folder of the supplied path.
GetFileVersion
Syntax: object.GetFileVersion(path)
This method is used to return the version of the file in the specified path.
GetFolder
Syntax: object.GetFolder (foldername)
This method returns a Folder object of the folder specified in the folder parameter.
GetParentFolderName
Syntax: object.GetParentFolderName(path)
Returns a string containing the name of the parent folder of the last file or folder in a specified path.
GetSpecialFolder
Syntax: object.GetSpecialFolder (folderid)
Returns the path to one of the special folders - \Windows, \System or \TMP.
GetTempName
Syntax: object.GetTempName
This method is used to generate a random filename for a temporary file.
MoveFile
Syntax: object.MoveFile source, destination
Moves one or more files from one location to another.
MoveFolder
Syntax: object.MoveFolder source, destination
Moves one or more folders from one location to another.
OpenTextFile
Syntax: object.OpenTextFile (filename [, iomode[, create[, format]]])
Opens the file specified in the filename parameter and returns an instance of the TextStreamObject for that file. )

VBScript » Objects » Err


VBScript » Objects » Err

The Err object holds information about the last runtime error that occured. It is not necessary to create an instance of this object; it is intrinsic to VBScript. Its default property is Number, which contains an integer representing a VBScript error number or an ActiveX control Status Code (SCODE) number. This value is automatically generated when an error occurs and is reset to zero (no error) after an On Error Resume Next statement or after using the Clear method.

Examples

Code:
<% 
dim numerr, abouterr 
On Error Resume Next 
Err.Raise 6 
numerr = Err.number 
abouterr = Err.description 
If numerr <> 0 Then 
Response.Write "An Error has occured! Error number " numerr " of the type '" abouterr "'." 
End If 
%>
Output:
"An Error has occured! Error number 6 of the type 'Overflow'."
Explanation:
This code checks the value of the Number property and, if it contains a value other than zero, displays the details in the browser.

Properties

Description
Syntax: object. Description [ =string]
This property returns or sets a string containing a brief textual description of an error.
HelpContext
Syntax: object.HelpContext [ =contextID]
This property is used to set or return a context ID for a Help topic specified with the HelpFile property.
HelpFile
Syntax: object. HelpFile [ = contextID]
This property is used to get or define the path to a Help file.
Number
Syntax: object.Number [ =errnumber]
This property is used to retrieve or set the value that relates to a specific runtime error.
Source
Syntax: object. Source [ =string]
This property lets us determine the object or application that caused an error, and returns a string that contains its Class name or programmatic ID.

Methods

Clear
Syntax: object.Clear
This method is used to clear teh settings of the Error object.
Raise
Syntax: object.Raise(number[, source, description, helpfile, helpcontext])
This method is used to generate a VBScript runtime error.

VBScript » Objects » TextStream


VBScript » Objects » TextStream

VBScript » Objects » Files Collection

Version: 3.0
The Files collection contains a set of File objects and is usually stored as a property of another object, such as a Folder object.

Examples

Code:
<% 
Dim filesys, demofolder, fil, filecoll, filist 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Set demofolder = filesys.GetFolder("foldername") 
Set filecoll = demofolder.Files 
For Each fil in filecoll 
filist = filist fil.name 
filist = filist "<BR>" 
Next 
Response.Write filist 
%>
Explanation:
This code demonstrates how to get a Files collection and list the contents in the browser.

Properties

Count
Syntax: object.Count
Returns an integer that tells us how many File objects there are in the collection.
Item
Syntax: object.Item ("filename") [= itemvalue]
The Item property allows us to retreive the value of an item in the collection designated by the specified key argument and also to set that value by using itemvalue.