Savandy
Savandy

Reputation: 393

Batch script to close desired Command Prompt windows

First, I have a script that starts 4 java programs:

@echo off
cd "C:\work\clientes\loremIpsum\loremIpsum\loremIpsum\target"
start "BROKER" java -jar loremIpsum-broker.jar

cd "C:\work\clientes\loremIpsum\loremIpsum\loremIpsum-virtual-devices-engine\target"
start "VIRTUAL-DEVICES-ENGINE" java -jar loremIpsum-virtual-devices-engine.jar

cd "C:\work\clientes\loremIpsum\loremIpsum\loremIpsum-websocket\target"
start "WEBSOCKET" java -jar loremIpsum-websocket.jar

cd "C:\work\clientes\loremIpsum\loremIpsum-loremIpsum\dandelion-workflow-engine\target"
start "WORKFLOW-ENGINE" java -jar loremIpsum-workflow-engine.jar
exit

I want to make a script that closes only these 4. I´ve tried with wmic:

@echo off
wmic process where "name like '%java%'" delete
exit

But I don't want it to close my other java aplications, only these 4

Upvotes: 0

Views: 80

Answers (1)

Compo
Compo

Reputation: 38579

The most likely unique and known commandline, would be its ending, e.g. -broker.jar. So the wildcard, % should represent the beginning of the commandline, i.e. %-broker.jar, (doubled in a batch file %%-broker.jar). You would therefore use the following:

WMIC Process Where "CommandLine Like '%%-broker.jar'" Call Terminate

I would suspect however that java.exe actually passes the initial command through a separate instance of cmd.exe, opening it in another window, which could be problematic, unless we knew what command it passed!

If the java command actually opens in a new cmd.exe window, with the window title you've stipulated, BROKER, you should be able to terminate it using TaskKill. Perhaps something like this:

TaskKill /F /FI "WindowTitle Eq BROKER" /T >Nul

Upvotes: 1

Related Questions