1

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

4

3 回答 3

2

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

于 2013-02-28T17:52:46.170 回答
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
于 2013-02-28T21:40:10.807 回答
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属性。

于 2013-02-28T21:14:33.400 回答