how do i kill an exe spawned by a batch file?

Hi, I have a batch file which runs a robocopy command.  I am using Windows task scheduler to run the scrpt nightly.  I have schueduled the batch file to run between 8pm and 8am when the users aren't around so that most files robocopy tries to copy are not in use.  

The problem I am having is that if task scheduler terminates the batch file at 8am the robocopy process is still left running.  Is there a way I can add to the batch file so that when the batch file closes the robocopy process it has spawned is also terminated?  

My batch file is below:

@echo off

set src="U:\source folder"
set dest="V:\destination folder"
set logname="C:\temp\Robocopy_Mirror.txt"

robocopy %src% %dest% /TBD /MT:32 /MIR /XJ /XF *.tmp /XD "$RECYCLE.BIN" "System Volume Information" /ZB /R:1 /W:1 /LOG+:%logname%

Open in new window



To make things more complicated I am running other robocopy jobs on the same computer so ideally I want to kill only the process spawned by this particular batch file.
carbonbaseAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
No, you do not have any way to force-close if the batch file is killed. In a batch you also don't know the PIDs of "child" processes. But you can use a particular user for the task, and kill all processes of that user at 8:00am.
Bill PrewIT / Software Engineering ConsultantCommented:
In Windows Scheduler, just run the following as the command to run, not a batch file.

robocopy "U:\source folder" "V:\destination folder" /TBD /MT:32 /MIR /XJ /XF *.tmp /XD "$RECYCLE.BIN" "System Volume Information" /ZB /R:1 /W:1 /LOG+:"C:\temp\Robocopy_Mirror.txt"

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
carbonbaseAuthor Commented:
Your solution seems to work well,  I had a slight issue because I had to map the drives first before the robocopy command, but I solved that by adding an action to map the drives before running the robocopy command.  I tested running multiple robocopy jobs on the same computer and only the robocopy process spawned by the schedule task is killed when I terminate the task.  

Thanks very much!
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that helped...

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.