0

I have a scheduled task that launches a VBScript with parameters which launches a hidden bat.

This bat executes a routine in a endless loop. The only way to be sure the bat is still running is from the TaskManager

enter image description here

If I want to close the batch I have to look for it there and kill the process.

Is there any other way to check if that batch is running or set a name to that process so I can look for it.

My Script code:

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run """Path to bat"" " & WScript.Arguments.Item(0) & " """ & WScript.Arguments.Item(1) & """", 0
Set WshShell = Nothing
user692942
  • 16,398
  • 7
  • 76
  • 175
Oiproks
  • 764
  • 1
  • 9
  • 34
  • 2
    look at `taskkill /?`, espcially `WINDOWTITLE` then in your batch file after `@echo off` add `title Sometitle` where sometitle will be the window title to search for. – Gerhard Feb 08 '22 at 10:55
  • How can I give a title to a hidden window that has been launched by a vbs script? – Oiproks Feb 08 '22 at 10:56
  • 2
    Here's an example. in the `batch-fle` do `title Test` then run the `vbscript`, open `cmd` and run `taskkill /FI "WINDOWTITLE eq Test"` obviously that will kill the process, but to simply view it, `tasklist` will return the result. – Gerhard Feb 08 '22 at 10:59
  • Perfect. That works like a charm. I can check if the bat is already running with `WshShell.Run "taskkill /fi ""WINDOWTITLE eq BatTitle""", , True` and kill it before running a new one. Thanks – Oiproks Feb 08 '22 at 11:07
  • 2
    Does this answer your question? [Killing processes in Vbscript](https://stackoverflow.com/questions/3300583/killing-processes-in-vbscript) – user692942 Feb 08 '22 at 13:18

1 Answers1

2

set a title in the batch file:

title Test

Create another batch-file, let's call it killer.cmd:

@echo off
setlocal enabledelayedexpansion
tasklist /FI "WINDOWTITLE eq Test" | findstr "cmd.exe"
if "%errorlevel%" equ "0" (
    Choice /c YN /M "kill process?"
    if "!errorlevel!" equ "1" taskkill /FI "WINDOWTITLE eq Test"
) else (
    echo Window Title does not exist.
)

To see if the process exists (by title) run killer.cmd which will list the title and if found, it will prompt if you want to kill it by using choice

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 1
    Nice! I was about to suggest to use multiple filters (`/FI`) for `tasklist` before I recognised that this command does not set its exit code when no matching processes are found… – aschipfl Feb 08 '22 at 13:52
  • Thanks, first time I realized this (the "hard") way was a few years back, @aschipfl. but that is a story for another day `:)` – Gerhard Feb 08 '22 at 13:57