Custom Search

Monday, March 30, 2009

Changing the Data in Excel sheets from all cloumns and rows

Option Explicit
Rem We use "Option Explicit" to help us check for coding mistakes
Rem the Excel Application

Dim objExcel
Rem the path to the excel file
Dim excelPath
Rem how many worksheets are in the current excel file
Dim worksheetCount
Dim counter
Rem the worksheet we are currently getting data from
Dim currentWorkSheet
Rem the number of columns in the current worksheet that have data in them
Dim usedColumnsCount
Rem the number of rows in the current worksheet that have data in them
Dim usedRowsCount
Dim row
Dim column
Rem the topmost row in the current worksheet that has data in it
Dim top
Rem the leftmost row in the current worksheet that has data in it
Dim left
Dim Cells
Rem the current row and column of the current worksheet we are reading
Dim curCol
Dim curRow
Rem the value of the current row and column of the current worksheet we are reading
Dim word
Rem find the value like "Testing\ATS" with in the Cell Data
Dim mypos
Rem split the Cell data
Dim myarray
Rem Find the Local Machine Name
Dim WshNetwork
Dim Computername
set WshNetwork= WScript.CreateObject("Wscript.Network")
Computername = WshNetwork.Computername
Rem where is the Excel file located?
excelPath = "C:\Documents and Settings\krao\Desktop\Inputs.xls"
'WScript.Echo "Reading Data from " & excelPath
Dim oFSO , objFile

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = oFSO.GetFile(excelPath)
' Checks for file attributes. This is a check for file being readonly. As the latests is taken from VSTS it will be marked as readonly. This code will uncheck the Readonly property
If objFile.Attributes AND 1 Then
objFile.Attributes = objFile.Attributes - 1
End If
Rem Create an invisible version of Excel
Set objExcel = CreateObject("Excel.Application")
Rem don't display any messages about documents needing to be converted
Rem from old Excel file formats
objExcel.DisplayAlerts = 0
Rem open the excel document
objExcel.Workbooks.open excelPath ', True , true
Rem How many worksheets are in this Excel documents
workSheetCount = objExcel.Worksheets.Count
'WScript.Echo "We have " & workSheetCount & " worksheets"
Rem Loop through each worksheet
For counter = 1 to workSheetCount
'WScript.Echo "Reading data from worksheet " & counter & vbCRLF
Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(counter)
Rem how many columns are used in the current worksheet
usedColumnsCount = currentWorkSheet.UsedRange.Columns.Count
Rem how many rows are used in the current worksheet
usedRowsCount = currentWorkSheet.UsedRange.Rows.Count
Rem What is the topmost row in the spreadsheet that has data in it
top = currentWorksheet.UsedRange.Row
Rem What is the leftmost column in the spreadsheet that has data in it
left = currentWorksheet.UsedRange.Column
Set Cells = currentWorksheet.Cells
Rem Loop through each row in the worksheet
For row = 1 to (usedRowsCount-1)
Rem Loop through each column in the worksheet
For column = 0 to usedColumnsCount-1
Rem only look at rows that are in the "used" range
curRow = row+top
Rem only look at columns that are in the "used" range
curCol = column+left
Rem get the value/word that is in the cell
word = Cells(curRow,curCol).Value
Rem find the value like "Testing\ATS" with in the Cell Data
mypos=inStrRev(word,"Testing\ATS")
If mypos>=1 Then
Rem split the Cell data
myarray=Split(word,"ATS",-1,1)
If myarray(0)="D:\Testing\" Then
Rem Change the Data "D:\Testing" to "C:\Testing"
myarray(0)="C:\Testing\"
Rem Add Total Cell Data
word=myarray(0)&"ATS"&myarray(1)
Rem Enter the Modified Data in Cell
Cells(curRow,curCol).Value=word
End If
End If
Rem Change the Cell Data(machine name) other then Local machine name
If Ucase(word)= "ALP-QASIM13-2K3" Then
Cells(curRow,curCol).Value=Computername
End If
Rem display the column on the screen
'WScript.Echo (word)
Next
Next
Rem We are done with the current worksheet, release the memory
Set currentWorkSheet = Nothing
Next
objExcel.workbooks(1).save
objExcel.Workbooks(1).Close
objExcel.Quit
Set currentWorkSheet = Nothing
Rem We are done with the Excel object, release it from memory
Set objExcel = Nothing

Thursday, March 26, 2009

Get the folders from "C" Drive

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFldr = objFSO.GetFolder("C:\")
Set Fldrs = objFldr.SubFolders
For Each Fldr In Fldrs
WScript.Echo Fldr
Next

Change the mode of the file Read only to Write

strApptEnterPath = "C:\Documents and settings\krao\Desktop\environment.xml"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = oFSO.GetFile(strApptEnterPath)
If objFile.Attributes AND 1 Then
objFile.Attributes = objFile.Attributes - 1
End If

Write the QTP Results in color



check Image for Code(just click on Image for Visible)

Wednesday, March 25, 2009

Kill the Process Through QTP

Killprocess = "Process name" per ex : calc.exe
Set ProcessList = GetObject("winmgmts://.").InstancesOf("win32_process")
For Each Process In ProcessList

If Process.Name = KillProcess Then
Process.Terminate
End IfNext

File Rename through QTP

dim spath,fso,f,sname
if count(wscript.arguments)<>0 then
spath=wscript.arguments(0)
set fso=createobject("scripting.filesystemobject")
'this is in the rightclick menu, so it can spare further test for existence, otherwise, you should.
if fso.fileexists(spath) then
set f=fso.getfile(spath)
sname=f.name
f.name=sname & ".original"
set f=nothing
end if
set fso=nothing
end if

Get the Results from Command prompt through QTP

Dim wShell, exec, outStr
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec( "%comspec% /C ping google.com" )
Do While exec.Status = 0 
 If Not exec.StdOut.AtEndOfStream Then 
     outStr = exec.StdOut.ReadAll 
     If exec.ExitCode = 1 then
          msgbox "Command failed" & "-" & outStr   
   End If   
 Exit Do 
End If 
If Not exec.StdErr.AtEndOfStream Then  
  outStr = "STDERR: " & exec.StdErr.ReadAll  
  msgbox "Command failed" & "-" & outStr
   Exit Do 
End If
 'Wait 1
Loop


***************************************************
Type-2
***************************************************
SystemUtil.Run "cmd.exe","","D:\WINDOWS\system32","open"
window("object class:=ConsoleWindowClass").Type "Dir"
window("object class:=ConsoleWindowClass").Type micReturn
window("object class:=ConsoleWindowClass").Maximize
wait(1)
txt= window("object class:=ConsoleWindowClass").GetvisibleText()
print txt

window("object class:=ConsoleWindowClass").Type "Exit"
window("object class:=ConsoleWindowClass").Type micReturn  



Source:http://www.advancedqtp.com/working-with-dos-command-line/

Stop the Services through QTP

strComputer = "."
strService = " 'W32Time' "
Set objWMIService = GetObject("winmgmts:"& _ "{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")Set colListOfServices = objWMIService.ExecQuery("Select * from _ Win32_Service Where Name ="& strService & " ")
For Each objService in colListOfServices
objService.StopService()
Next

Note : here strService is Services Name which you are going to start. you get this name from control panel\Administrative Tools\Services... and right click and select properties on the Service , here you can find Service Name.

Start the Services through QTP

strComputer = "."
strService = " 'W32Time' "
Set objWMIService = GetObject("winmgmts:"& _ "{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")Set colListOfServices = objWMIService.ExecQuery("Select * from _ Win32_Service Where Name ="& strService & " ")
For Each objService in colListOfServices
objService.StartService()
Next

Note : here strService is Services Name which you are going to start. you get this name from control panel\Administrative Tools\Services... and right click and select properties on the Service , here you can find Service Name.

Changing System Date and time through QTP

newTime="03:40:00"
newDate=Date+1
Dim oShell
set oShell=CreateObject("WScript.Shell")
oShell.run "cmd /k date" &CStr(" "&newDate)&"& exit"
oShell.run "cmd /k time" &CStr(" "&newTime)&"& exit"
set oShell=nothing
curDateTime=now
msgbox curDateTime

Tuesday, March 24, 2009

Uninstall a Software Using VBScript , QTP

This works for uninstalling most applications. In this example I will show how AutoCAD 2005 can be uninstalled using a VBScript. Open Regedit and look in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall search for the application and find a the product code that is in GUID format looking like this: {5783F2D7-0301-0409-0002-0060B0CE6BBA} It might be something else for you.

The code below will uninstall AutoCAD 2005 as well as Express Tools. Copy the code below into Notepad and give the file the extension vbs.

on error resume next

Set WshShell = CreateObject(”WScript.Shell”)

‘ Uninstall Express Tools

WshShell.Run “msiexec /x {5783F2D7-0311-0409-0000-0060B0CE6BBA} /q”,1,true

‘ Uninstall AutoCAD 2005

WshShell.Run “msiexec /x {5783F2D7-0301-0409-0002-0060B0CE6BBA} /q”,1,true