VBS: Check for When A Exe Closes

Discussion in 'Software' started by Heaney, Sep 23, 2011.

  1. Heaney

    Heaney Private E-2

    Hello.

    After running an exe (that has to be downloaded first, but is then run) from a vbs file, is it possible to check for when the exe closes and then echo a bit of text?

    Remember that this exe file is called from a url, so it has to be downloaded first and that'll take a while, so it probably would also need to check for when it'd start in the first place.

    And remember, when downloaded it'd go into a temp location, so we can't really specify that when checking... or can you? I don't know.

    I'm thinking you should just check for whether it's running.
     
    Last edited: Sep 23, 2011
  2. GermanOne

    GermanOne Guest

    Untested:
    Code:
    Const strFileName = "C:\wherever\file.ext"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    For i = 1 To 60
      If objFSO.FileExists(strFileName) Then
        Set objFSO = Nothing
        WScript.Echo "File was found."
        WScript.Quit
      End If
      WScript.Sleep 1000
    Next
    Set objFSO = Nothing
    WScript.Echo "Timeout."
    
    Regards
    GermanOne

    EDIT:
    Wait, you changed your requirement.
     
  3. Heaney

    Heaney Private E-2

    Sorry!

    I didn't expect even you to be so quick to respond :-o
     
  4. GermanOne

    GermanOne Guest

    Nevermind ;)

    Next attempt:
    Code:
    Const strProcessName = "notepad.exe"
    
    Set oWMISvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    boolRunning = False
    
    ' Loop while the process is not running
    Do While Not boolRunning
      Set colProc = oWMISvc.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & strProcessName & "'")
      For Each oProc In colProc
        boolRunning = True
      Next
      Set colProc = Nothing
      WScript.Sleep 500
    Loop
    
    ' Loop while the process is running
    Do While boolRunning
      Set colProc = oWMISvc.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & strProcessName & "'")
      boolRunning = False
      For Each oProc In colProc
        boolRunning = True
      Next
      Set colProc = Nothing
      WScript.Sleep 500
    Loop
    
    WScript.Echo "Ready."
    
    As you can see I tested with Notepad. You have to find out the process which is running (use the task-manager if you don't know it).

    Regards
    GermanOne
     
  5. Heaney

    Heaney Private E-2

    Thanks!

    But I forgot to say, it should only tell the user when the program has ended ONCE per time this script is run, not every time it closes.

    EDIT: Nevermind, I see you've done that anyways.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds