确定最后完成的脚本

时间:2013-02-28 17:39:59

标签: vbscript

我有几个在目录中运行的脚本。它们都是同时执行的。我无法确定最后完成哪个脚本。有没有办法找到它?

3 个答案:

答案 0 :(得分:2)

使用主脚本通过.Exec启动(子)脚本;监视exec对象的状态属性;记录/显示exec对象状态变为WshFinished的时间。

答案 1 :(得分:2)

基本日志记录将完成工作,在脚本开始和结束时写入日志条目,您也可以记录持续时间。您可以将结果写入日志文件。结果是在几秒钟内。

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

答案 2 :(得分:1)

我的建议是基于WMI。 (滚动下面的新主意)

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

[编辑]嗯,现在我想到了一个新想法,也许你会发现它很有趣,或者至少试一试。

' 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

我们的想法是通过在文件中附加一个字符来更改文件DateLastModified属性。