0

I have three batch files ,

open.bat

run.bat

process.bat

open.bat calls run.bat and run.bat calls process.bat .. All the three are keep running , i know there is a way to kill cmd.exe .. But, my problem is how to kill particular cmd.exe which running my batcg files ?

In my case i need to kill open.bat when process.bat is closed. I am noob in this, So, guide me as much as detail u can. Thank U.

cmd.exe / open.bat

cmd.exe / run.bat

cmd.exe / process.bat

how to mention the particular cmd.exe to kill it ? using vbs or cmd ?

Lots of thanks in advance.. :)

Philip
  • 686
  • 3
  • 13
  • 32
  • This is already what happens. How are you starting each batch file from the previous one? Typically open would wait for run and run would wait for process to close. Once process closes, control is returned to run, when run closes control is returned to open, then open closes. – Appleoddity Oct 06 '17 at 05:08
  • i have starting each batch files using call when open.bat calls process.bat , the open.bat will keep running to give commands for run.bat , So all the three files are required to keep running in my case. But, if user close/kill process.bat it need to kill open.bat and run.bat must need to keep running.. any solution for this ?? – Philip Oct 06 '17 at 05:13
  • @DavidPostill , Is that PID is same on all user PC who are all running my batch ? i think mentioning PID will only works on my own PC !! pls clarify.. – Philip Oct 06 '17 at 05:15
  • @Philip I don't understand your comment. Every process has it's own PID. – DavidPostill Oct 06 '17 at 05:19
  • I mean , How to auto detect that particular program PID using batch file itself . When i running my batch file in some other PC is it gives the same PID ? for example is it possible to kill particular batch file named open.bat , instead of killing whole comman cmd.exe ? – Philip Oct 06 '17 at 05:34
  • 1
    Unlike Unix where running a shell script creates a new shell process by default, on Windows calling a batch file or multiple nested batch files is done within one CMD.EXE process. To get a 'particular' process you can kill separately you must start CMD or a file that associates to CMD (which .bat files normallly do) and start can also set the new window title to a value you can then filter on (unless /b skips it). – dave_thompson_085 Oct 07 '17 at 06:03
  • @dave_thompson_085 , Thank u, i Understood nearly. If i use start command instead of call it will open new window of cmd.exe with separate title, i hope am right. If so, then my question is how to kill that particular process opened cmd.exe window with new title ? – Philip Oct 07 '17 at 07:49
  • If you use start (without /b) it creates a new window. The new window has a unique title if you specify a title that is unique; do start /? for syntax. If the new window has a unique title you can kill it by filtering on that title as explained in the Q @DavidPostill linked to. – dave_thompson_085 Oct 08 '17 at 00:02
  • @Philip That is explained in my answer Kill specific instance of process on windows. You need to use start "my unique title" ... – DavidPostill Oct 08 '17 at 00:17

1 Answers1

0

In vbscript, you can do like this :

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
'**************************************************************************
Hackoo
  • 1,404