从另一个脚本调用最近添加的脚本

时间:2015-08-24 14:59:42

标签: vbscript

只要将文件添加到脚本文件夹,就会被此代码检测到。

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
    & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

我希望在从此VBScript添加到脚本文件夹后立即执行VBScript。怎么做?获取添加到脚本文件夹然后执行该VBScript的文件的名称。

2 个答案:

答案 0 :(得分:2)

在检测到新文件时,将此代码行替换为您想要执行的代码:Wscript.Echo objLatestEvent.TargetInstance.PartComponent。例如,下一个代码片段显示了一种可能的方法(这就是为什么有宽Echo输出,比必要的更宽......):

''''(unchanged code above)
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    ''''Wscript.Echo objLatestEvent.TargetInstance.PartComponent
    Call DoWithName( objLatestEvent.TargetInstance.PartComponent)
Loop

Sub DoWithName( strPartComp)
  Dim arrFileName
  arrFileName = Split( strPartComp, """")
  If True Or UBound(arrFileName) > 0 Then
    Wscript.Echo strPartComp _
    & vbNewLine & UBound( arrFileName) _
    & vbNewLine & "[" & arrFileName( 0)  & "]" _
    & vbNewLine & "[" & arrFileName( 1)  & "]" _
    & vbNewLine & "[" & arrFileName( 2)  & "]" _
    & vbNewLine & ShowAbsolutePath( arrFileName( 1))
  End If
End Sub

Function ShowAbsolutePath( strPath)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   ShowAbsolutePath = fso.GetAbsolutePathName( strPath)
End Function

请注意

  • ShowAbsolutePath( arrFileName( 1))返回添加到脚本文件夹的文件名;现在你可以
  • 检查它是否是有效的.vbs文件名,如果是,则在任一Windows Script Host引擎(wscript.execscript.exe)中组合启动它

答案 1 :(得分:1)

您可以尝试修改此脚本:

If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else 
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
    & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name=""d:\\\\scripts""'")

    Do
        Set objLatestEvent = colMonitoredEvents.NextEvent
        Call DoWithName(objLatestEvent.TargetInstance.PartComponent)
    Loop
End if
' --------------------------------------
Sub DoWithName( strPartComp)
    Dim Title,arrFileName,Question,ws
    Title = "Execute vbscript"
    set ws = CreateObject("wscript.shell")
    arrFileName = Split( strPartComp, """")
    If True Or UBound(arrFileName) > 0 Then
        Wscript.Echo strPartComp _
        & vbNewLine & UBound( arrFileName) _
        & vbNewLine & "[" & arrFileName( 0)  & "]" _
        & vbNewLine & "[" & arrFileName( 1)  & "]" _
        & vbNewLine & "[" & arrFileName( 2)  & "]" _
        & vbNewLine & DblQuote(ShowAbsolutePath(arrFileName(1)))
    End If
    Question = MsgBox("Did you want to execute this vbscript : " & DblQuote(ShowAbsolutePath(arrFileName(1))),vbYesNo+vbQuestion,Title)
    If Question = vbYes Then
        ws.run DblQuote(ShowAbsolutePath(arrFileName(1)))
    Else
    End if
End Sub
' --------------------------------------
Function ShowAbsolutePath( strPath)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    ShowAbsolutePath = fso.GetAbsolutePathName( strPath)
End Function
' --------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
' --------------------------------------
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
' --------------------------------------
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function  
' --------------------------------------