Tuesday, April 27, 2010

Few Sample script for VB

To sun any application form CMD (command prompt / MS DOS) one has to use either “cscript” or “Wscript” to provide interpreter environment to script to get execute Sample script to open & read excel sheet: Dim objExcel Set objExcel = WScript.CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open("C:\test1.xls") objWorkbook.Activate Dim intRow , intCol intRow = 1 : intCol = 1 WScript.Echo " Coloumn : Data " Do Until objExcel.Cells(intRow,intCol).Value = "" Do Until objExcel.Cells(intRow, intCol) = "" WScript.Echo objExcel.Cells(intRow, intCol).Value intRow = intRow + 1 Loop WScript.Echo " " intCol = intCol + 1 intRow = 1 Loop objExcel.Workbooks.Close () objExcel.Quit Sample script to work around Drive (C:\, D:\,etc): Dim filesys set filesys = CreateObject("Scripting.FileSystemObject") Set drv = filesys.GetDrive("e") select case drv.DriveType Case 0: drtype = "Unknown" Case 1: drtype = "Removable" Case 2: drtype = "Fixed" Case 3: drtype = "Network" Case 4: drtype = "CD-ROM" Case 5: drtype = "RAM Disk" End Select WScript.Echo "The specified drive is a " & drtype & " type disk." WScript.Echo " " WScript.Echo "Total size is " & drv.TotalSize & ". " WScript.Echo "Available space is " & drv.AvailableSpace & ". " WScript.Echo "Type is " & drv.DriveType & " " WScript.Echo "Path is " & drv.Path & " " Dim filesyst, drive Set filesyst = CreateObject("Scripting.FileSystemObject") WScript.Echo "Before Condtion" drive = filesyst.DriveExists("z") WScript.Echo drive If filesyst.DriveExists("z") Then WScript.Echo("The specified drive does exist.") Else WScript.Echo("The specified drive does not exist.") End If Sample script for file handling: 'This program will create a file E:\file_location.txt, write and read the date and then it will delete' 'it' Dim fso, file, file_location, FileName file_location = "E:\file_location.txt" Const ForReading = 1, ForWriting = 2, ForAppending = 8 Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile("E:\file_location.txt", ForWriting, True) file.Write("This is a place to get all your qtp") file.Write("questions and answers solved.") Set file = fso.OpenTextFile("E:\file_location.txt", ForReading, True) Do while file.AtEndofStream <> True data = file.ReadLine() msgbox data Loop file.Close() fso.DeleteFile("E:\file_location.txt") Sample script to open any application: Set objShell = WScript.CreateObject("WScript.Shell") 'ObjShell.Exec("C:\Program Files\Internet Explorer\IEXPLORE.EXE") Set IE = CreateObject("InternetExplorer.Application") IE. IE.visible = 1 IE.navigate("https://" & URL & "www.google.com") Friends let me know if you want any specific generic script I would really happy to help.

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | JCpenney Printable Coupons