Como matar cmd.exe em particular por lote ou vbs?

0

Eu tenho três arquivos em lote,

open.bat

run.bat

process.bat

open.bat chamadas run.bat e run.bat chamadas process.bat .. Todos os três estão em execução, eu sei que há uma maneira de matar cmd.exe .. Mas, meu problema é como matar cmd particular .exe que executa meus arquivos batcg?

No meu caso, preciso matar open.bat quando process.bat está fechado. Eu sou noob nisso, Então, me guie o máximo que puder. Obrigado U.

cmd.exe / open.bat

cmd.exe / run.bat

cmd.exe / process.bat

como mencionar o cmd.exe particular para matá-lo? usando vbs ou cmd?

Muito obrigado antecipadamente ..:)

    
por Philip 06.10.2017 / 07:06

1 resposta

0

Em vbscript, você pode fazer assim:

Option Explicit
Dim Process2Check, Process2Kill
Process2Check = "D:\process.bat"
Process2Kill = "D:\open.bat"
If AppPrevInstance() Then   
    MsgBox "Instance already running",VbExclamation,"Instance already running"    
    WScript.Quit   
Else   
    Do   
        Call Main(Array(Process2Check))
        Call Pause(1) 
    Loop   
End If   
'**************************************************************************
Sub Main(colProcessPaths)   
    Dim ProcessPath   
    For Each ProcessPath In colProcessPaths     
        CheckProcess(ProcessPath)   
    Next   
End Sub   
'**************************************************************************
Sub CheckProcess(ProcessPath)   
    Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
            If .Count = 0 Then    
                Call Kill(Process2Kill)
            Else    
                Exit Sub    
            End if   
        End With   
    End With   
End Sub   
'**************************************************************************
Sub Kill(Process2kill)   
    Dim ProcessName : ProcessName = StripProcPath(Process2kill) 
    Dim Item,colItems  
    Set colItems = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\.\root\cimv2")_   
        .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))  
        For each Item in colItems 
            If colItems.Count <> 0 Then    
                Item.TERMINATE
                WScript.Quit
            End if    
        Next     
End Sub   
'**************************************************************************
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   
'**************************************************************************
Sub Pause(Sec)    
    Wscript.Sleep(Sec*1000)    
End Sub   
'**************************************************************************
Function StripProcPath(ProcessPath)   
    Dim arrStr : arrStr = Split(ProcessPath, "\")   
    StripProcPath = arrStr(UBound(arrStr))   
End Function   
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
'Function to add doubles quotes into a variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
    
por 20.10.2017 / 01:22