Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Tuesday, June 2, 2009

Advanced VB Script Samples

'********************************************************************************
'Write a program to read data from a text file
'********************************************************************************
 
'Read Text File 
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,1)
'Reading Complete Data from File
MsgBox fl.ReadAll
 
'********************************************************************************
'Write a program to write data into a text file
'********************************************************************************
 
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,2)
'Write characters
fl.Write("hello")
'Write blank lines
fl.WriteBlankLines(2)
'Write data line by line 
fl.WriteLine("A New Line")
 
'********************************************************************************
'Write a program to print all lines that contains a word either “testing” or “qtp”
'********************************************************************************
 
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,1)
'Read Data Line by Line
While Not fl.AtEndOfStream
If instr(1,fl.ReadLine,"testing")<>0 or instr(1,fl.ReadLine,"qtp")<>0 then
print fl.ReadLine
End if
Wend
 
'********************************************************************************
'Write a program to print the current foldername
'********************************************************************************
 
Set fso=CreateObject("scripting.filesystemobject")
msgbox fso.GetAbsolutePathName("")
 
'********************************************************************************
'Write a program to print files in a given folder
'********************************************************************************
 
Dim fso,fld,sFiles,sFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(FolderPath)
Set sFiles = fld.Files
For Each sFile in sFiles
print sFile.name
Next
 
'********************************************************************************
'Write a program to print subfolders in a given folder
'********************************************************************************
 
Dim fso,fld,sfolders,sFld
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(FolderPath)
Set sfolders = fld.SubFolders
For Each sFld in sfolders
msgbox sFld.name
Next
 
'********************************************************************************
'Write a program to print all drives in the file system
'********************************************************************************
Set fso = CreateObject("Scripting.FileSystemObject")
Set drvs = fso.Drives
 
For Each drv in drvs
print drv.DriveLetter
Next
 
'********************************************************************************
'Write a program to print current drive name
'********************************************************************************
 
Set fso = CreateObject("Scripting.FileSystemObject")
msgbox fso.GetDriveName(fso.GetAbsolutePathName(""))
 
'********************************************************************************
'Print the last modified and creation date of a given file
'********************************************************************************
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.GetFile(FilePath)
print fl.DateLastModified
print fl.DateCreated
 
'********************************************************************************
'Print the size of the file 
'********************************************************************************
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.GetFile(FilePath)
msgbox  fl.Size
 
'********************************************************************************
'Write a program to display files of a specific type
'********************************************************************************
 
Function DisplaySpecificFileTypes(FolderPath,FileType)
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(FolderPath)
Set sFiles = fld.files
For Each sFl in sFiles
If lcase(sFl.type)=lcase(FileType) then
print sFl.name
End if
Next
 
End Function
 
'Calling the Function
DisplaySpecificFileTypes "C:\","Text Document"
 
'********************************************************************************
'Write a program to print the free space in a given drive
'********************************************************************************
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvPath)
print "Free Space: " & FormatNumber(d.FreeSpace/1024, 0) 
 
'********************************************************************************
'Write a program to find whether a given folder is a special folder
'********************************************************************************
 
fldPath="C:\WINDOWS"
Const WindowsFolder =0
Const SystemFolder = 1
Const TemporaryFolder = 2
 
Set fso = CreateObject("Scripting.FileSystemObject")
If lcase(fso.GetSpecialFolder(WindowsFolder))=lcase(fldPath) or lcase(fso.GetSpecialFolder(SystemFolder))=lcase(fldPath) or lcase(fso.GetSpecialFolder(TemporaryFolder))=lcase(fldPath) then
print "Given Folder is a special Folder"
Else
print "Given Folder is not a special Folder"
End if
 
'********************************************************************************
'Write a program to remove all empty files in the folder
'********************************************************************************
 
FolderPath="C:\Documents and Settings\sudhakar kakunuri\Desktop\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(FolderPath)
Set sFiles = fld.Files
For Each sFile in sFiles
 
If sFile.size=0 Then
print sFile.name
End If
Next
 
'********************************************************************************
'Write a program to Copy contents of one folder to other folder
'********************************************************************************
 
Function CopyContentstoOtherFolder(SourceFolder,TargetFolder)
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(SourceFolder)
 
Set sFiles = fld.Files
 
For Each sFile in sFiles
sFile.copy TargetFolder&"\"&sFile.name
Next
 
Set sFlds = fld.SubFolders
 
For Each sFld in sFlds
sFld.copy TargetFolder&"\"&sFld.name
Next
 
End Function
 
'Calling the Function
CopyContentstoOtherFolder "C:\Documents and Settings\sudhakar kakunuri\Desktop\Test1","C:\Documents and Settings\sudhakar kakunuri\Desktop\Test2"
 
'********************************************************************************
'Write a program to check whether a given path represents a file or a folder
'********************************************************************************
 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fPath) then
Print "Path Representing a File"
Elseif fso.FolderExists(fPath) then
Print "Path Representing a Folder"
End if
 
'********************************************************************************
 
'********************************************************************************
'Write a program to compress a folder
'********************************************************************************
 
strComputer = "."
strFolder   = "Folder Path"
 
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objFolder = objWMI.Get("Win32_Directory='" & strFolder & "'")
 
fCompress = objFolder.Compress  ' To uncompress change this to objFolder.Uncompress
 
if fCompress <> 0 then
WScript.Echo "There was an error compressing the folder: " & fCompress
else
WScript.Echo "Folder compression successful"
end if
 
'********************************************************************************
'Write a program to rename a folder
'********************************************************************************
 
CreateObject("Scripting.FileSystemObject").GetFolder("C:\Documents and Settings\sudhakar\Desktop\Training Session For MF Testers").Name="New Name"
 
'********************************************************************************
'Write a program to print all lines in a file that ends with “world”
'********************************************************************************
 
Set fso=CreateObject("Scripting.FileSystemObject")
Set fl=fso.OpenTextFile(FilePath)
 
Set regEx = New RegExp
regEx.Pattern = "world$"
regEx.Global = True
 
While Not fl.AtEndOfStream
Set Matches = regEx.Execute(strng)
 
If Matches.count<>0 then
print fl.ReadLine
End if
 
Wend
 
'********************************************************************************
'Write a program to check whether string starts with “Error”
'********************************************************************************
str="Errorhello"
Set regEx = New RegExp
regEx.Pattern = "^Error"
regEx.Global = True
Set Matches = regEx.Execute(str)
 
If Matches.count<>0 then
msgbox "String Started with 'Error'"
Else
msgbox "String Not Started with 'Error'"
End if
 
'********************************************************************************
'Write a program to Replace all words that contains “demo” in a file with the word “QTP”
'********************************************************************************
FilePath="C:\Documents and Settings\sudhakar\Desktop\demo.txt"
 
Set fso=CreateObject("Scripting.FileSystemObject")
Set fl=fso.OpenTextFile(FilePath)
txtToReplace=replace(fl.ReadAll,"demo","QTP")
fl.Close
 
Set fl=fso.OpenTextFile(FilePath,2)
fl.Write(txtToReplace)
fl.Close
'********************************************************************************
'Write a program to check whether a string contains only alpha numerics
'********************************************************************************
 
str="xyz123!!"
Set regEx = New RegExp
regEx.Pattern = "[^A-Za-z0-9]"
regEx.Global = True
Set Matches = regEx.Execute(str)
 
If Matches.count=0 then
msgbox "String Contains only alpha numerics"
Else
msgbox "String Contains other characters"
End if
'********************************************************************************
'Write a program to check whether given string is in date format
'********************************************************************************
MyDate = "October 19, 1962"
 
If isdate(MyDate) Then
msgbox "Given String is in Date Format"
else
msgbox "Given String is not in Date Format"
End If
 
'********************************************************************************
'Write a program to Invoke command prompt and execute dir command from the shell
'********************************************************************************
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /K CD C:\ & Dir"
Set oShell = Nothing
'********************************************************************************

2 comments :

  1. your code to print line having word "qtp" or "testing" is not correct.

    write code is
    Set fso=CreateObject("scripting.filesystemobject")
    Set fl=fso.OpenTextFile(FilePath,1)
    'Read Data Line by Line
    While Not fl.AtEndOfStream
    linetoprint=f1.readline
    If instr(1,linetoprint,"testing")<>0 or instr(1,linetoprint,"qtp")<>0 then
    print linetoprint
    End if
    Wend

    ReplyDelete
    Replies
    1. correct. two readline statements should not be there in single line.

      Delete