Two code to invoke the program with the VBS and monitor the operation of the program _vbs

Source: Internet
Author: User
Tags sleep
At the same time to use a CAE software to invoke the external program, but, through the CAE software to invoke the external program, because the external program has parameters, call the method written in the bat file, by the CAE software to call this bat, so, the CAE software can not be used to monitor the procedures invoked, The calling program is still running, the preparation is not finished, to the next process, so, resulting in process errors, I wrote two VBS code, through which to call to solve the problem! Two small programs are monitored in different ways!

Two programs are called differently, one is to monitor the process of the system, one is to monitor the file features generated by the program!

The first one:

' This program is used in conjunction with Simcode to invoke other programs, and to monitor the operation of the program!
' The monitoring method is: Find the log file of the program, if there is an end sign, the program has been completed!
Set wshshell=wscript.createobject ("Wscript.Shell")
Wshshell.run ("notepad.exe c:\kw.txt") ' called Program
Wscript.Sleep 2000′ wait 2 seconds.
Dim Fso,ts,i
Do when not i=1′ when I=1 is not established, always loop!
Set Fso=wscript.createobject ("Scripting.FileSystemObject")
Set ts=fso.opentextfile ("c:\kw.txt") ' Open log file of the program that was invoked
Do and not TS. AtEndOfStream ' When there is no end to file loop
Data=ucase (Trim (ts.readline)) ' reads a row in the log file
If InStr (data, "OK") Then ' Find out if the contents of the read above is a sign that the program is running! The flag in this example is "OK"
i=1′ when you get the sign that the program is running, pass a value to do loop, make it end loop, no longer monitor log file
Exit do
End If
Loop
Set ts = Nothing
Set fso = Nothing ' Close created object
Wscript.Sleep 2000′ wait 2 seconds for the next round of log file monitoring
Loop
WScript.Echo "Ok!" Give the user a feedback, call the program execution completed! When you are in real use, you should not do this line!
The second one:



' This program is used in conjunction with Simcode to invoke other programs, and to monitor the operation of the program!
' The monitoring method is: monitoring the system process
Set WshShell = WScript.CreateObject ("Wscript.Shell")
Allapp= "C:\windows\system32\notepad.exe" writes down the full path of the program to be invoked.
Appname= "notepad.exe" writes down the name of the program that was invoked.
Wshshell.run (Allapp) ' Start the invoked program

Dim AppPath
For every PS in GetObject ("Winmgmts:\\.\root\cimv2:win32_process"). Instances_ ' lists all running programs in the system
If LCase (ps.name) =appname Then ' detects whether a program exists in the process
Apppath=ps.commandline ' The command line for the extraction program
End If
Next

Do ' Loop detection
Myqqin=chkuin (APP) ' detects whether the command line exists in the process!
If not Myqqin then ' if there is no running then, tell the user and end the monitor!
MsgBox "The invoked program has exited!" "' In actual use, please take out this line!
Exit do
Else
Wscript.Sleep 3000′ wait 5 seconds.
End If
Loop ' Back to continue detection

function Chkuin (APP)
For each PS in GetObject ("Winmgmts:\\.\root\cimv2:win32_process"). Instances_
If LCase (ps.name) =appname Then
Apppatht1=ps.commandline
If Apppatht1=apppath then Chkuin=true End If
End If
Next
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.