Kashif
Kashif

Reputation: 14430

How to stop batch file running in background started using .vbs script

I have a file invisible.vbs having following script

Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("e:\abc.bat", 0)
set WshShell = Nothing

abc.bat has following script

:loop
EVENTCREATE /T ERROR /L APPLICATION /ID 100 /D "This is  test message."
ping localhost -n 21 > nul
goto loop

Now I want to stop abc.bat manually but Question is HOW?

Upvotes: 1

Views: 8428

Answers (4)

FlameBlazer
FlameBlazer

Reputation: 1698

If you have it open in command prompt you can press CTRL + C. Wait a few seconds and it will have terminated.

Upvotes: 0

James K
James K

Reputation: 4055

To do it from a .bat file, first add this to abc.bat:

 TITLE=KILLME

Then run this command:

taskkill /fi "windowtitle eq KILLME"

You can set the title to anything you want. KILLME is just an example.

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

The answer is: it depends. Do you want to stop it manually just this once? In that case go with the answer @BaliC already gave.

If you want to add an option to your script to allow for termination of the background loop, you're going to need something like this, and you need to spawn the background process via Exec(), not via Run(). That way you can keep a handle that will allow you to terminate the process:

Set sh = CreateObject("WScript.Shell")
Set p = sh.Exec("%COMSPEC% /c e:\abc.bat")

' IE dialog creation goes here

Do While p.Status = 0
  WScript.Sleep 100
  If ie.document.all("continue").Value = "no" Then p.Terminate
Loop

Upvotes: 2

Bali C
Bali C

Reputation: 31231

Go into task manager and end the cmd.exe process.

Upvotes: 0

Related Questions