Luke101
Luke101

Reputation: 65308

Determine which script finished last

I have several script that run in a directory. They all are executed simultaneously. I am having trouble determining which script finished last. Is there a way to find this out?

Upvotes: 1

Views: 103

Answers (3)

Panayot Karabakalov
Panayot Karabakalov

Reputation: 3179

My suggestion is based on WMI. (scroll below for fresh idea)

Option Explicit

Dim objWMIService, colProcesses, objProcess
Dim iCount, iLoop, sFileName

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & ".\root\cimv2")

' wait all scripts to finish
Do While True
    WScript.Sleep 200
    ' snapshot running scripts
    Set colProcesses = objWMIService.ExecQuery( _
        "Select * From Win32_Process " _
        & "Where Name = 'WScript.exe' " _
        & "OR Name = 'CScript.exe'", , 48)

    iCount = 0
    For Each objProcess In colProcesses
        ' skip current "monitor" script, test the rest
        If InStr (objProcess.CommandLine, WScript.ScriptName) = 0 Then
            sFileName = Split(objProcess.CommandLine, """")(3)
            iCount = iCount + 1
        End If
    Next
    If iCount < 1 Then Exit Do
    iLoop = iLoop + 1
Loop

' and show what we get
If iLoop > 0 Then
    WScript.Echo "LastOne:" & vbNewLine & sFileName
Else
    WScript.Echo "No other .vbs running except Me"
End If

[EDIT] Well, a new idea come to my mind right now, maybe you'll find it interesting, or at least give it a try.

' do your work here...
WScript.Sleep 3000

Call SelfLogged

Sub SelfLogged()
    Const ForAppending = 8
    With CreateObject("Scripting.FileSystemObject")
        With .OpenTextFile(WScript.ScriptFullName, ForAppending)
            .Write Chr(0)
        End With
    End With
End Sub

The idea is to change file DateLastModified property by appending a char to the file.

Upvotes: 1

peter
peter

Reputation: 42207

Basic logging will do the job, write a log entry when the script starts and ends, you can also log the duration. You could write the results to a logfile. The results are in seconds.

startTime=timer
wscript.echo "started at " & startTime
'do your stuff'
wScript.sleep 500
stopTime=timer
wscript.echo "stopped at " & StopTime & " duration was " & stopTime - startTime

'started at 81558,17
'stopped at 81558,67 duration was 0,5

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38765

Use a master script to start the (child) scripts via .Exec; monitor the status properties of the exec objects; log/display the time when the exec object's status change to WshFinished.

Upvotes: 2

Related Questions