user145078
user145078

Reputation: 251

Stop a batch file using another batch file?

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?

Upvotes: 4

Views: 15246

Answers (3)

Whome
Whome

Reputation: 10400

I have implemented SendKey.exe utility(Delphi console app) which emulates key presses but its a part of the internal application. I tried the following command and it worked fine. App uses keybd_event Win32 calls.

SendKey.exe -wn "TestLoop.bat" -ks "{CTRL+C}Y{ENTER}"

You could use NirCmd sending keypresses I think should work.

Upvotes: 0

Kumar
Kumar

Reputation: 3990

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

Upvotes: 4

Stephan
Stephan

Reputation: 56180

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"

Upvotes: 1

Related Questions