Stop a batch file using another batch file?

13,325

Solution 1

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

Solution 2

There is no thing like a "please do something"-message to an batchfile. You will have to emulate it, for example like this:

REM mybatch.bat
:loop
REM do useful things
if not exist "c:\triggerfile.tmp" goto :loop

del "c:\triggerfile.tmp"
REM do "postprocessing"
exit

In your main bat file instead of taskkill just echo x>"c:\triggerfile.tmp"

Share:
13,325
user145078
Author by

user145078

Updated on June 28, 2022

Comments

  • user145078
    user145078 almost 2 years

    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?