26 Dec 2012

Vb Script Advanced Programming

Write a program to read data from a text file

'The below mentioned code is used to read the data from a text file using QTP

'Define the variables
Dim objFSO, objFile, strLine

'Create an object of File System
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open an existing text file in read mode
set objFile = objFSO.OpenTextFile("D:\DataFile.txt", 1)

'Read the file till end of the stream
Do Until objFile.AtEndOfStream

'Read the line
strLine = objFile.ReadLine

'Print the read value on the Report
Reporter.ReportEvent micDone, strLine, strLine

'Pop up the message read value
msgbox strLine

Loop

'Close the connection with stream
objFile.Close

'Destroy the objFile object
Set objFile = Nothing

'Destroy the objFSO object
Set objFSO = Nothing

Write a program to write data into a text file


You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the Writeline method automatically inserts a new line character while the Write method doesn’t insert a new line character.


Set fso=createobject("Scripting.filesystemobject")

set file= fso.CreateTextFile("D:\WriteData.txt", ForWriting, True) 

'//2nd argument should always be "ForWriting" in order to write contents to a file

file.Writeline("Welcome to the QTP") 

file.Writeline("Welcome to the Prime Ki Software solutions pvt ltd")

Write a program to print the current foldername

Dim f,Fso

set Fso=CreateObject("Scripting.FileSystemobject")

set f=Fso.Getfolder("D:\QTP\QTP Scripts")

Print f.Name

or


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("D:\QTP\Qtp Material")

Set sFiles = fld.Files

For Each sFile in sFiles

 Print sFile.name

Next

Write a program to print all lines that contains a word either “testing” or “qtp” 

Set fso=CreateObject("Scripting.Filesystemobject")



Set fl=fso.OpenTextFile("D:\QTP\Qtp Material\WRvsQTP from site.txt",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 subfolders in a given folder

Dim fso,fld,sfolders,sFld

Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder("D:\QTP\Qtp Material")

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("D:\QTP\Qtp Material\WRvsQTP from site.txt")

print fl.DateLastModified

print fl.DateCreated

Print the size of the file and size on the disk


Set fso = CreateObject("Scripting.FileSystemObject")

Set fl = fso.GetFile("D:\QTP\Qtp Material\WRvsQTP from site.txt")

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
            msgbox sFl.name
        End if
    Next

End Function

'Calling the Function
DisplaySpecificFileTypes "C:\","New Microsoft Office Excel Worksheet"

Write a program to print the free space in a given drive


Set fso = CreateObject("Scripting.FileSystemObject")

Set d = fso.GetDrive("C:\")

msgbox "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

Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder("D:\Prime")

Set sFiles = fld.Files

For Each sFile in sFiles

    If sFile.size=0 Then

        sFile.delete

    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 "D:\test1","D:\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

set fso=CreateObject("Scripting.FileSystemObject")

set folder=fso.GetFolder("D:\Prime")

folder.Name="venkat"


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



No comments:

Post a Comment