Two codes that call a program with VBS and monitor the running status of the program

Source: Internet
Author: User

There is also a need to use a CAE software to call external programs, but after calling the external program through this CAE software, because this external program has parameters, the call method is written in the Bat file, the CAE software calls the Bat. Therefore, the CAE software cannot monitor the called program, and the called program is still running. The preparation is not complete, so the next process is complete, therefore, a process error occurs. I wrote two VBS codes and used them to call the code to solve this problem! The two applets are monitored in different ways!

The calling methods of the two programs are different. One is to monitor the system process and the other is to monitor the file features generated by the program!


'The program is used to call other programs with SimCode and monitor the running status of the program ,!
'Monitoring method: Find the program's Log file. If there is an end sign, it indicates that the program has been executed!
Set WshShell = Wscript. CreateObject ("Wscript. Shell "){notepad.exe c: \ kw.txt ") 'called Program
Wscript. sleep 2000 'Wait 2 seconds
Dim fso, ts, I
Do While Not I = 1' when I = 1 is Not successful, keep repeating!
Set fso = Wscript. CreateObject ("Scripting. FileSystemObject ")
Set ts = fso. opentextfile ("c: \ k1_txt") 'Open the log file generated by the called program on a daily basis.
Do While Not ts. AtEndOfStream 'loops when the end of the file is Not reached
Data = ucase (trim (ts. readline) 'reads a row in the log file
If instr (data, "OK") then', check whether the content read above has a sign that the program is running completely! In this example, the winning candidate is "OK"
I = 1' when the program running is completed, a value is passed to the Do loop so that it ends the loop and no longer monitors Log files.
Exit Do
End If
Set ts = nothing
Set fso = nothing 'close the created object
Wscript. sleep 2000 'Wait 2 seconds before the next round of Log File monitoring
Wscript. echo "OK !" 'Give the user a feedback. The called program has been executed! This line should not be used in actual use!

'The program is used to call other programs with SimCode and monitor the running status of the program ,!
'Monitoring method: Monitoring System Processes
Set WshShell = Wscript. CreateObject ("Wscript. Shell ")
AllApp = "c: \ windows \ system32 \ notepad.exe" 'write down the complete path of the program to be called.
Appnamedomainnotepad.exe "'writes down the name of the called program.
WshShell. Run (allApp) 'Start the called Program

Dim AppPath
For each ps in getobject ("winmgmts :\\ root \ cimv2: win32_process"). instances _ 'lists all running programs in the system.
If lcase (ps. name) = AppName then 'checks whether a program exists in the process
AppPath = ps. commandline 'extract the command line of the program
End if

Do 'loop detection
Myqqin = chkuin (App) 'checks whether the command line obtained above exists in the process!
If not myqqin then, if it is not running, tell the user and stop monitoring!
Msgbox "The called program has exited !" 'In actual use, please remove this line!
Exit do
Wscript. sleep 3000 'Wait 5 seconds
End if
Loop 'returns 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
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: 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.