Showing posts with label Vbs. Show all posts
Showing posts with label Vbs. Show all posts

Monday, January 21, 2013

Text File Operations in Vbs

Example :

Dim sPath
set fso=createobject("scripting.filesystemobject")
sPath=inputbox("enter path where you want to save the file")

fso.createtextfile sPath
set ctrl=fso.getfile("1.txt")
set mode=ctrl.openastextstream(8)
'1-read
'2-write
'8-append
mode.writeline "hi"
mode.writeblanklines 5
mode.writeline "end"
msgbox "over"
set fso=nothing

Sorting using built in Function

Example :

set a =createobject("system.collections.arraylist")
a.add "5"
a.add "1"
a.add "2"
a.sort
for each i in a
 b=b&i&vbnewline
next
msgbox b

Vb Script --Regular Expression EXAMPLES

1)To validate any website address starting from --www

set regex=new regexp
regex.pattern="www\..*\.(com|in|jp)$"
n=inputbox("validate any website address starting from --www")
if regex.test(n) then
 msgbox "valid"
else
 msgbox "invalid"
End if

2) Replace all matches in the string with the replacement text

Sub ReplaceAllByExpression(ByRef StringToExtract, ByVal MatchPattern, _ByVal ReplacementText)

 Dim regEx, CurrentMatch, CurrentMatches
 Set regEx = New RegExp
 regEx.Pattern = MatchPattern
 regEx.IgnoreCase = True
 regEx.Global = True
 regEx.MultiLine = True
 StringToExtract = regEx.Replace(StringToExtract, ReplacementText)
 Set regEx = Nothing

End Sub

' Example
' Remove all images from an HTML page
ReplaceAllByExpression HTMLPageData, "<img[^<]*?>", ""

The RexExp Object


The RegExp object has three properties and three methods. The properties are:
  1. Pattern property - holds the regular expression pattern
  2. Global property - True or False (default False). If False, matching stops at first match.
  3. IgnoreCase property - True or False (default True). If True, allows case-insensitive matching
The methods are:
  1. Execute method - executes a match against the specified string. Returns a Matches collection, which contains a Match object for each match. The Match object can also contain a SubMatches collection
  2. Replace method - replaces the part of the string found in a match with another string
  3. Test method - executes an attempted match and returns True or False

To set up a RegExp object:

Dim re
Set re = New RegExp
With re
    .Pattern = "some_pattern"
    .Global = True
    .IgnoreCase = True
End With

A Pattern can be any string value.
 For example, if the pattern is "Hello World", the RegExp object will match that in the target string.
If IgnoreCase is True, it will match any case, so "hellO wORld" would be matched.

If Global is set to True, it will contine to search the string for all instances of "Hello World". If False, it will stop searching after the first instance is found.

Execute method, returning Matches collection

Dim re, targetString, colMatch, objMatch
Set re = New RegExp
With re
  .Pattern = "a"
  .Global = True
  .IgnoreCase = True
End With
targetString = "The rain in Spain falls mainly in the plain"

Set colMatch = re.Execute(targetString)
For each objMatch  in colMatch
  Response.Write objMatch.Value & "<br />"
Next


The above will produce a list of 5 letter a's.


Test method, returning True or False

Dim re, targetString
Set re = New RegExp
With re
  .Pattern = "a"
  .Global = False
  .IgnoreCase = False
End With
targetString = "The rain in Spain falls mainly in the plain"

re.Test(targetString)

Monday, January 14, 2013

Using Class in Vbs

Here is a piece of code which using class in vbs, to add numbers 2 and 3:-


Call a2()

Class a1

             Private Function add2()
                        msgbox 2+3
            End Function

            Function add1()
                        Call add2()
            End Function
End Class

Function a2()
         Set a2=new a1
         a2.add1
End Function

'Save the above code in text file and save as ".vbs"