Wednesday, November 5, 2014

VBScript Important Statements

Option Explicit    ' To Force Develoer to declare the variable with DIM statment, if we don't declare the variables then the interpreter will throw and error.

Dim result,i,vartype,Msg,objw

result = " VB Version Number :" & ScriptEngine &" "& ScriptEngineMajorVersion &"."& ScriptEngineMinorVersion &"."& ScriptEngineBuildVersion & vbcrlf

Dim arr01(8)
arr01(0) = "711"
arr01(1) = "Raj Kumar"
arr01(2) = 60000    
arr01(3) = 4.25  
arr01(4) = #11/04/1981#
arr01(5) = #08.45 AM#  
arr01(6) = True
arr01(7) = Array ("One","Two","Three")



result = result &" Variable Type Name: " &vbcrlf


For each i in arr01

 vartype= typename(i) ' typename is to get the Type of the Variable

 result = result & "IsEmpty = "& isEmpty (i) & vbcrlf
 result = result & "IsNull = "& IsNull (i) & vbcrlf
 result = result & "IsObject = "& IsObject (i) & vbcrlf
 result = result & "IsNumeric = "& IsNumeric (i) & vbcrlf




 result = result & " Type  = "& vartype & vbcrlf & vbcrlf

Next

'MsgBox result

Msg =  "Vbscript" & vbCrLf & "Programming"
Set objW = CreateObject("Word.Application")
 objW.Visible = True


 With objW ' We can use With...End With... statement to select the default object for set of statments.
     .Documents.Add
     .Selection.TypeText result ' TypeText is to Type the text in document
     .Selection.WholeStory ' WholeStory is for select All
 End With

VBScript File System (FSO) - 1

Dim FSO, drive, space ,dc,t

Set FSO = CreateObject("Scripting.FileSystemObject")       'CreateObject to create the File System Object

set drive= FSO.drives     ' To fetch All the drives detail from the system

Result= "Drives in system = " & vbcrlf

For Each i in drive

    Select Case i.DriveType     ' To get the Drive Type

          Case 0: t = "Unknown"
          Case 1: t = "Removable"
          Case 2: t = "Fixed"
          Case 3: t = "Network"
          Case 4: t = "CD-ROM"
          Case 5: t = "RAM Disk"
      End Select

   Result= Result& "Drive " & i.DriveLetter & ": - " & t & vbcrlf



      temp = ""
      Result= Result& i.DriveLetter & " - "    'DriveLetter is to get th Driver Letter like C for C:\
      If i.DriveType = 3 Then
         temp = i.ShareName
      ElseIf i.IsReady Then
         temp = i.VolumeName
      Else
         temp = "Drive not available"
      End If
      Result= Result& temp &  vbcrlf

Set drive = FSO.GetDrive(FSO.GetDriveName(i))
Result = Result & "Drive " & UCase(drvPath) & " - "
Result = Result & drive.VolumeName   & "  "& vbcrlf
Result = Result & "Free Space: " & FormatNumber(drive.FreeSpace/1024/1024, 2)
Result = Result & " Mbytes" & vbcrlf
Result = Result & "Total Space: " & FormatNumber(drive.TotalSize/1024/1024, 2) & vbcrlf
Result = Result & " Mbytes" & vbcrlf & vbcrlf



Next

Result= Result& vbcrlf


MsgBox Result

Tuesday, November 4, 2014

VBScript Function and Sub Procedures

Function is a group of reusable statements (lines of code) which can be called anywhere in your program. This eliminates the need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions. Apart from inbuilt Functions, VBScript allows us to write user-defined functions as well. 


'Sub procedures
'Sub Procedures are similar to functions but there are few differences.
'Sub procedures DONOT Return a value while functions may or may not return a value.
'Sub procedures Can be called without call keyword.
'Sub procedures are always enclosed within Sub and End Sub statements.


Function welcome()

MsgBox "Welcome to Functions"

End Function


Function AreaofRect1(x,y)

MsgBox "Area of the Rectangle is " & x*y

End Function



Function AreaofRect2(x,y)

Dim area : area = x * Y
AreaofRect2= area    'If you want to Return the result, assigned to be retured value to the function name itself

End Function


Sub AreaofCircle(x)

MsgBox "Area of Circle is "& 3.141 *x*x

End Sub



Call Welcome()

Call AreaofRect1(12,5)

result =  "Area of the Rectangle is " & AreaofRect2(12,15)

MsgBox result

Call AreaofCircle(4)
AreaofCircle(5)

VBScript Array Functions

'Array Functions
'LBound This function will return an integer for the smallest subscript of the given arrays.
'UBound This function will return an integer for the Largest subscript of the given arrays.
'Split This function will return an array that contains a specified number of values. Splitted based on a Delimiter.
'Join This function will return a String that contains a specified number of substrings in an array. This is an exact opposite function of Split Method.
'Filter This function will return a zero based array that contains a subset of a string array based on a specific filter criteria.
'IsArray This function will return a boolean value that indicates whether or not the input variable is an array.
'Erase This function will recover the allocated memory for the array variables.

Dim x
Dim arr01(5)
arr01(0) = "231"           'String
arr01(1) = "Adam"          'String
arr01(2) = 13000   'Number
arr01(3) = 5.45   'Decimal Number
arr01(4) = #11/07/1982#    'Date
arr01(5) = #11.05 AM#      'Time

Result = " Aray = arr01(5) " & vbcrlf
Do

Result= Result & "arr01("& x &") :- "& arr01(x) & vbcrlf
x=x+1
Loop While (x <= UBound(arr01))

Result = Result & "The smallest Subscript value array is : " & LBound(arr01)& vbcrlf
Result = Result & "The Biggest Subscript value array is : " & UBound(arr01)& vbcrlf

Result = Result & "Join of Array arr01 : " & join(arr01)& vbcrlf

MsgBox Result





'---------------------------------------------------------------------



Str="Adam,Jay,Robert,Juliana,Ajay"

Dim arr3
Result = " Result of Arr3 which made by split function on string : ''Adam,Jay,Robert,Juliana,Ajay''" & vbcrlf
arr3=Split(str,",")

x=0
Result= Result & "arr3 IsArray :- " & IsArray(arr3) & vbcrlf
Do

Result= Result & "arr3("& x &") :- "& arr3(x) & vbcrlf
x=x+1
Loop While (x <= UBound(arr3))



MsgBox Result





----------------------------------------------------


' For MultiDimension Arrays :
Dim arr02(3,2)
Result = " Aray = arr02(3,2) " & vbcrlf
Result = Result &  "The smallest Subscript of arr02 is : " & LBound(arr02,1) & vbcrlf
Result = Result &"The smallest Subscript of arr02 is : " & LBound(arr02,2)

MsgBox Result




'-----------------------------------------------------------

Str="Adam*Jay*Robert*Juliana*Ajay"
arr3=Split(str,"*")


fresult1 = Filter (arr3,"A") 'Adam,Ajay
fresult2 = Filter (arr3,"A",0) '0 means exclude so result will be Jay,Robert,Juliana
fresult3 = Filter (arr3,"A",1) '1 means include so result will Adam,Ajay
fresult4 = Filter (arr3,"ja",1,0) '1,0 means include and search based on Binary (case sensitive) so result will be only Ajay
fresult5 = Filter (arr3,"ja",1,1) '1,0 means include and search based on Text (case insensitive) so result will be Jay,Ajay

Result = " Filter Result on Array items Adam,Jay,Robert,Juliana,Ajay  " & vbcrlf


Result= Result & "Filter (arr3,''A'')"& vbcrlf

For Each x in fresult1

Result= Result & x  & vbcrlf

Next

Result= Result & vbcrlf

Result= Result & "Filter (arr3,''A'',0)"& vbcrlf
For Each x in fresult2

Result= Result & x  & vbcrlf

Next

Result= Result & vbcrlf
Result=  Result & "Filter (arr3,''A'',1)"& vbcrlf
For Each x in fresult3

Result= Result & x  & vbcrlf

Next

Result= Result & vbcrlf
Result= Result & "Filter (arr3,''ja'',1,0)"& vbcrlf
For Each x in fresult4

Result= Result & x  & vbcrlf

Next

Result= Result & vbcrlf
Result=  Result & "Filter (arr3,''ja'',1,1)"& vbcrlf

For Each x in fresult5

Result= Result & x  & vbcrlf

Next

MsgBox result






Erase arr3        ' Each element is reinitialized.

VBScript Arrays

Arrays are sequence of memory, where collection of values can be stored. In VB we can store different type of values in a Array.


'Array can be Single Dimenstion OR Multi Dimension

'Although, the Array size is indicated as 5, it can hold 6 values as array index starts from ZERO.

'Array Index Cannot be Negative.

'VBScript Arrays can store any type of variable in an array. Hence, an array can store an integer, string or characters in a single array variable.




'Declaration of an Array
'Declaration Type 1 : Mentioning the Size
Dim arr01(6) ''Declared with size of 6

' In VBScript Array can store any type of variable.

arr01(0) = "711"         'String
arr01(1) = "Raj Kumar"   'String
arr01(2) = 60000     'Number
arr01(3) = 4.25 'Decimal Number
arr01(4) = #11/04/1981#  'Date
arr01(5) = #08.45 AM#    'Time



data = "Data From Array arr01"& vbcrlf

For each i In arr01

data= data & i & vbcrlf

Next

MsgBox data




-------------------------------------------------------------

'Declaration Type 2 :


Dim arr02()  'Without Size OR dynamic-array

ArrSize = 2
ReDim arr02(ArrSize) ' ReDim is a statement to declare dynamic-array size and allocate or reallocate storage space.
arr02(0) = "745"
arr02(1) = "Salman"

data1 = "Data From Array arr02 after 1st declaration"& vbcrlf

For i=0 To UBound(arr02)

data1= data1 & "arr02("&i&"): "& arr02(i) & vbcrlf

Next




ArrSize = 4                ' Rr Declare the size of the Array
ReDim arr02(ArrSize)   ' herre when we will reaalocate the Array the previous values of array will get clear. to Avoid this we can use PRESERVE
arr02(2) = 55000
arr02(3) = 5.24

data2 = vbcrlf& vbcrlf& "Data From Array arr02 after Re declare"& vbcrlf

For i=0 To UBound(arr02)

data2= data2 & "arr02("&i&"): "& arr02(i) & vbcrlf

Next

MsgBox data1 & vbclrf & data2


ArrSize = 6                ' Rr Declare the size of the Array
ReDim PRESERVE arr02(ArrSize)   ' herre when we will reaalocate the Array the previous values of array will get clear. to Avoid this we can use PRESERVE
arr02(0) = "745"
arr02(1) = "Salman"
' we are not re assigning values to arr02(2) and (3) because we want to preserve it from existing
arr02(4) = #11/04/1976#
arr02(5) = #09.45 PM#

data3 =  vbcrlf& vbcrlf&  "Data From Array arr02 after Re declare with PRESERVE"& vbcrlf

For i=0 To UBound(arr02)

data3= data3 & "arr02("&i&"): "& arr02(i) & vbcrlf

Next

MsgBox data1 & vbclrf & data2 & vbclrf & data3





------------------------------

'Declaration Type 3 : using 'Array' Parameter
Dim arr03
arr03 = Array("One","Two","Three")

data3 =  "Data From Array arr03"& vbcrlf

For Each i  In arr03

data3= data3 & i & vbcrlf

Next

MsgBox data3







------------------------------------------------------------------------
'Array can be Single Dimenstion OR Multi Dimension 

'Although, the Array size is indicated as 5, it can hold 6 values as array index starts from ZERO.

'Array Index Cannot be Negative.

'VBScript Arrays can store any type of variable in an array. Hence, an array can store an integer, string or characters in a single array variable.



Dim arr(2,3) ' Array which has 3 (0,1,2) rows and 4(0,1,2,3) columns

For x =0 To 2 

For y=0 To 3
arr(x,y)= "("& x &","& y & ")"
Next

Next


data =" Data From Array (2D)"& vbcrlf

For x =0 To 2 

For y=0 To 3
data = data& "  " & arr(x,y)
Next

data = data & vbcrlf

Next

MsgBox data



Monday, November 3, 2014

VBScript Operators

Operator which perform operations on operands. Where operands can be variable,string or some other object.

'VBScript language supports following types of operators:
'1. Arithmetic Operators
'2. Comparison Operators
'3. Logical (or Relational) Operators
'4. Concatenation Operators


Dim a 'Declaration

a=20 'Initialisation
b=3 'Declaration and Initialisation

MsgBox "A  = "& a & " B =" & b

MsgBox "A + B = "& a+b

MsgBox "A - B = "& a-b

MsgBox "A * B = "& a*b

MsgBox "A / B = "& a/b

MsgBox "A MOD B = "& a MOD b

MsgBox "A ^ B = "& a^b

---------------------------------------------------------------------------------





Dim a 'Declaration

a=10 'Initialisation
b=20 'Declaration and Initialisation

MsgBox "A  = "& a & " B =" & b

MsgBox "A == B = "& a = b

MsgBox "A <> B = "& a <> b

MsgBox "A > B = "& a > b

MsgBox "A < B = "& a < b

MsgBox "A >= B = "& a >= b

MsgBox "A <= B = "& a <= b


----------------------------------------------------------------------




Dim a 'Declaration 

a=10 'Initialisation
b=0 'Declaration and Initialisation

Result = ""

If a<>0 AND b<>0 Then                    
     Result = Result &"AND Operator Result is : True"
     Result = Result & vblf 'Inserting a Line Break for readability
  Else
     Result = Result &"AND Operator Result is : False"
     Result = Result & vblf 'Inserting a Line Break for readability
  End If

  If a<>0 OR b<>0 Then
     Result = Result &"OR Operator Result is : True"
     Result = Result & vblf
  Else
     Result = Result &"OR Operator Result is : False"
     Result = Result & vblf
  End If

  If NOT(a<>0 OR b<>0) Then
     Result = Result &"NOT Operator Result is : True"
     Result = Result & vblf
  Else
     Result = Result &"NOT Operator Result is : False"
     Result = Result & vblf
  End If

  If (a<>0 XOR b<>0) Then
     Result = Result &"XOR Operator Result is : True"
     Result = Result & vblf
  Else
     Result = Result &"XOR Operator Result is : False"
     Result = Result & vblf
  End If

MsgBox Result


------------------------------------------------------------------------




Dim a : a = 5
Dim b : b = 10
Dim c
Dim Result : Result = ""

  c=a+b
  Result = Result & "Concatenated value:1 is " &c 'Numeric addition
  Result = Result & vblf 'Inserting a Line Break for readability
  c=a&b
  Result = Result &"Concatenated value:2 is " &c  'Concatenate two numbers
  Result = Result & vblf 'Inserting a Line Break for readability

MsgBox Result


VBScript Constant Variable

Constant is providing a facility to declare a variable Constant. System will give run time error If user will try to initialize again.

'Constant is a named memory location used to hold a value that CANNOT be changed during the script execution.
'If a user tries to change a Constant Value, the Script execution ends up with an error.
'Constants are declared the same way the variables are declared.

Dim Var1

var1=20

Const ConsVar1 =25
'Const Dim ConsVar2 =25 ' Expected Identifier

'Const ConsVar3 ' Expected =
'ConsVar3=30


MsgBox " Normal Variable Before change "&Var1
MsgBox " Constant Variable "&ConsVar1

var1=20

'ConsVar1=20 'illegal assignment

MsgBox " Normal Variable After change "&Var1

VBScript Scope of Variables

' Scope of Variables

'DIM
'Variables declared using “Dim” keyword at a Procedure level are available only within the same procedure. Variables declared using “Dim” Keyword at script level are available to all the procedures within the same script.
'Example : In the below example, the value of Var1 and Var2 are declared at script level while Var3 is declared at procedure level.


'PRIVATE
'Variables that are declared as "Private" have scope only within that script in which they are declared. When declaring a variable of type "Private", Dim keyword is replaced by "Private".
'Example : In the below example, Var1 and Var2 are available at Script Level. Var3 is declared as Private and it is available only for this particular script. Use of "Private" Variables is more pronounced within the Class.

'PUBLIC
'Variables declared using "Public" Keyword are available to all the procedures across all the associated scripts. When declaring a variable of type "public", Dim keyword is replaced by "Public".
'Example : In the below example, Var1 and Var2 are available at script level while Var3 is available across the associated scripts and procedures as it is declared as Public.

Dim Var1
Dim Var2
Public Var4
Private var5

DispVar()

Function DispVar()
Var1=101
Var2=102
vsr5=105

Dim Var3
Var3 = Var1+Var2

Var4=1001

MsgBox " Inside Function Var3: "&Var3
MsgBox " Inside Function Var4: "&Var4
MsgBox " Inside Function Var5: "&Var5

End Function



MsgBox "Outside Function Var1: "&Var1 ' Displays 101 as Var1 is declared at Script level
MsgBox "Outside Function Var2: "&Var2 ' Displays 102 as Var1 is declared at Script level
MsgBox "Outside Function Var3: "&Var3 ' Var3 has No Scope outside the procedure. Prints Empty
MsgBox "Outside Function Var4: "&Var4 ' Displays 1001 and it is accessable out of this script also.
MsgBox "Outside Function Var5: "&Var5 ' Displays 105 but Var3 is available only for this script.

VBScript Rules for Declaring Variables

' Rules for Declaring Variables

'Rules for Declaring Variables:
' Variable Name must begin with an alphabet.
' Variable names cannot exceed 255 characters.
' Variables Should NOT contain a period(.)
' Variable Names should be unique in the declared context.

'Assigning Values to the Variables
' The numeric values should be declared without double quotes.
' The String values should be enclosed within doublequotes(")
' Date and Time variables should be enclosed within hash symbol(#)

Dim Name,DOB,EmpID

Name= "Raj Kumar"
DOB= #23-10-2014#
EmpID= 432


MsgBox "Name : "&Name

MsgBox "DOB : "&DOB

MsgBox "EmpID : "&EmpID

VBScript Carriage Return / Line feed / Next Line

' Basic program for carriage return / line feed / Next Line.


Dim fName 'Declaration

fName="Dinesh " 'Initialisation
lName= "Jangra" 'Declaration and Initialisation

EmpID= 988
MsgBox fName+LName  ' String + String = String
MsgBox fName&LName ' String & String = String

MsgBox fName & vblf &LName                      ' vblf is for next line

MsgBox fName & vbnewline &LName          ' vbnewline is for next line

MsgBox fName & vbcrlf &LName                 'vbcrlf is for next line

MsgBox fName & Chr(13) & Chr(10) &LName      'Chr(13) & Chr(10) The character codes for a carriage return and line feed