Monday, January 12, 2015

Working with File System Objects (FSO)

Option Explicit

Const DeleteReadOnly = True 
Dim oFSO, oDrive, sFileName

Set oFSO   = CreateObject("Scripting.FileSystemObject") 
sFileName  = ".exe"

For Each oDrive In oFSO.Drives 
  If oDrive.DriveType = 2 Then Recurse oFSO.GetFolder("F:\")
Next 

Sub Recurse(oFolder)
  Dim oSubFolder, oFile
wscript.echo oFolder.name
  If IsAccessible(oFolder) Then
    For Each oSubFolder In oFolder.SubFolders
     Recurse oSubFolder
    Next 

    For Each oFile In oFolder.Files
        sFileName = oFolder.name & ".exe"
      If oFile.Name = sFileName Then
        oFile.Delete ' or whatever
      End If
    Next 
  End If
End Sub

Function IsAccessible(oFolder)
  On Error Resume Next
  IsAccessible = oFolder.SubFolders.Cont >= 0
End Function

Saturday, January 10, 2015

Working With Dictionary Objects

Object that stores data key, item pairs.

A Dictionary object is the equivalent of a PERL associative array. 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 a integer or a string, but can be anything except an array.

Methods

Add Method (Dictionary) | Exists Method | Items Method | Keys Method | Remove Method | RemoveAll Method

Properties

Count Property | Item Property | Key Property

Dim d   ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"   ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Functions can return a dictionary object and it can indeed use to return multiple values from a Functions. Alternately Function can return multiple value using Array as well 


Dim dicObj
Set dicObj = CreateObject("Scripting.Dictionary")

Set obj=getname
MsgBox(obj.item("name"))


Public Function getname()
dicObj.add "name","Uday Kumar"
Set getname=dicObj
End function

Tuesday, January 6, 2015

Using Excel as Database

Option Explicit
Dim objConn,objRS
 
Set objConn = CreateObject("ADODB.Connection") 
Set objRS = Createobject("ADODB.Recordset")

objConn.Open "Driver={Microsoft Excel Driver (*.xls),(*.xlsx)(*.xlsm),(*.xlsb)};DBQ=C:\USERS\Desktop\FileName.xlsx;"
objRS.Open "Select [UN],[PW] FROM [TABLE1$]", objConn

Do while not objRS.EOF
 Browser("Gmail").WebEdit("username").Set objRS.FIELDS("UN")
 Browser("Gmail").WebEdit("password").Set objRS.FIELDS("PW")
 Browser("Gmail").WebBotton("LogIn").CLICK
 objRS.MOVENEXT
Loop

'Release created objects
Set objRS= NOTHING
Set objConn= NOTHING


Note: The database we are using here is MS Excel2007(*.xlsx). Create a table/sheet name in MS Excel2007 called "TABLE1" and column 'names as "UN" and "PW" and path of the table which we create is C:\USERS\Desktop\FileName.xlsx.