Stop a batch file using another batch file?

user145078 picture user145078 · Nov 26, 2014 · Viewed 12.6k times · Source

I have a batch file which starts a new batch file on a new cmd prompt using the following command:

C:\Windows\System32\cmd.exe /c "start mybatch.bat"

The mybatch.bat process keeps on running until someone stops it. When we close this batch file using the Ctrl+C signal, it does the operation of collecting the coverage data and then comes out. After initiating the mybatch file I am doing some other process on the parent batch file and then I want to stop the mybatch file.

I tried using taskkill for closing the process using the command in the parent batch file:

taskkill /fi "windowtitle eq c:\Windows\SYSTEM32\cmd.exe - mybatch.bat"

The problem here is that it stops the batch file forcefully not allowing it to run the coverage process which would have happened if I had used Ctrl+C manually. Any thoughts on how I could achieve stopping the mybatch file using the parent batch file?

Everything is done using a batch file. Any help is highly appreciated.

My main batch file looks something like:

start mybatch.bat
REM do something like copying files, running tests, etc
taskkill /fi "windowtitle eq c:\Windows\SYSTEM32\cmd.exe - mybatch.bat"

In the above code instead of doing taskkill what if I want to do Ctrl+C on the command prompt with windowtitle "c:\Windows\SYSTEM32\cmd.exe - mybatch.bat" using the main batch file. Is it possible?

Answer

Kumar picture Kumar · Nov 26, 2014

test.bat

@echo off
title mybatchfile
echo ...do something

testkill.bat

Taskkill /FI "WINDOWTITLE eq mybatchfile"

testkill.bat will kill the test.bat

But you have tried it already.

UPDATED

It cannot done to stop batch from another batch. But you can run a batch as service to set a process name then you can define what should do on start and stop. This service can kill from batch file.

see my answer here