Powered by: Dharamveer Saxena

Wednesday, January 30, 2013

How to check if a process is still running in your windows


Many times your script need to know if a program or for example a bat file is still running. so, how you can check it using VBScript.

Below is the code which you can use to check if a process/program is still running

sub p_check()
with createobject("shell.application")
 for each wndw in .windows
   locationurl_ = cstr(wndw.LocationURL)
   if InStr(locationurl_, "locationurl") = 0 then
'here in locationurl you can pass the name of the bat file
 
else
'to quit the bat file if needed after script found it
     wndw.quit
end if
next
end with
End sub


Here, using wndw object in for each loop you can check the process based on its application name as well in which it is open for example iexplorer but you need to use correct method to get values based on your need

Reading and writing in XML using QTP - Vbscript


Using VbScript one can read and write into an XML file by creating an object of "Msxml2.DOMDocument"



If one needs to read or write into this XML file, then it can be done using the below procedure where I have used "object_" and "data_file_path" as parameter.
1. object_ can be "_path" if you want to get values inside "_path" tag in above XML
2. data_file_path is XML file path like "c:\data.xml"

Sub Write_read_XML(object_, data_file_path)

'creating object for "Msxml2.DOMDocument"
       Set xmlDoc = CreateObject("Msxml2.DOMDocument")

'load xml file on object
       xmlDoc.load(data_file_path)

'creating an object which can store all values in tag: like in this case all the values inside object_ tag will be saved in ElemList
       Set ElemList = xmlDoc.getElementsByTagName(object_)

'Now loop through all the tags with name object_ in your XML file
       for n = 0 to 1

      'Now to fetch value you can use:
               msgbox ElemList.item(n).text

      'Incase you need to save values in XML then you can use the code:
               ElemList.item(n).text = "value " & cstr(n)

       next
End Sub