-4
start cmd /k 

/k: is compulsory which will execute.

launching many command propmts can be done as below.

start cmd /k Call rc_hub.bat 4444

start cmd /k Call rc_grid1.bat 5555

start cmd /k Call rc_grid1.bat 6666

start cmd /k Call rc_grid1.bat 5570.

I want to know what cmd to be used for closing after launching

Eg: if %a="1234" start cmd /k Call rc_grid1.bat --> This is opening cmd , what cmd needs to give to close ?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Why is `/K` compulsory? – Compo Mar 03 '18 at 10:20
  • Wait, you want to keep it open but close it? this is as unclear as it gets. – Gerhard Mar 03 '18 at 12:53
  • Using `/k` requests the shell to remain running in interactive mode. It shouldn't be closed. If you don't want an interactive shell, then use something like `start cmd /c "rc_hub.bat 4444 & waitfor.exe rcHubClose"`. Then have the parent signal it to close with `waitfor.exe /si rcHubClose`. The signal name can consist of ASCII letters a-z and A-Z, and digits 0-9; punctuation characters and underscore aren't allowed. – Eryk Sun Mar 03 '18 at 14:04
  • please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. – Rishav Mar 03 '18 at 17:42

2 Answers2

1

Closing multiple instances of cmd is not a very uncommon task. It can arise, for example, when debugging some complex set of batch scripts.

There is one of the ways to do this:
First, give cmd windows unique titles. For example,

start "unique_title" cmd.exe /k ...

Second, when you want to close them, get process ids from tasklist output matching the titles. Then kill those ids with taskkill.

tasklist /v /fo csv | findstr "unique_title"

Here is the full example, the first argument is the title substring to match:

kill_title.cmd

@echo off
set TITLE=unique_title
if not "%~1"=="" set "TITLE=%~1"
for /f "tokens=1,2,* delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr "%TITLE%"') do (
    rem here we check that the first column is "cmd.exe" ... just in case
    if "%%~a"=="cmd.exe" echo taskkill /pid "%%~b"
)

First check the script output, and if it is ok, remove echo before taskkill.

wolfrevokcats
  • 2,100
  • 1
  • 12
  • 12
  • taskkill.exe supports "IMAGENAME" and "WINDOWTITLE" filters in its Unicode command-line. From a standpoint of internationalization, using those options would limit this to only a single decode operation when CMD reads the "kill_title.cmd" script. CMD uses the console output codepage for this, which defaults to OEM and can be changed via chcp.com, line by line in a script. In contrast, searching with findstr.exe is potentially a problem, since it lossily encodes its command line as ANSI and then lossily translates this to OEM and searches input text as bytes, implicitly as OEM text. – Eryk Sun Mar 05 '18 at 01:27
  • WINDOWTITLE can only match from the beginning and when we use `start "title" ...` current user name is added to the title which makes WINDOWTITLE useless. As to findstr, we can always choose a title that is ASCII and unique enough to not trigger unwanted matches. – wolfrevokcats Mar 05 '18 at 11:05
  • The current user name is not added to the title. If the user is an administrator, the CMD shell prepends a localized name, which is message ID 0x40002748 in cmd.exe.mui: "Administrator: %0\r\n" (English), "Administrador: %0\r\n" (Spanish), and "管理者: %0\r\n" (Japanese). The console itself doesn't add this, so `SetConsoleTitle` works to set an exact title, but it's no help since CMD resets the title after an external program runs. The genius who decided to do this should get a reward. It adds nothing useful. Admin access could already be checked with `whoami /groups`. – Eryk Sun Mar 05 '18 at 11:53
  • Well, you're right about username, I didn't check this thoroughly, I just wanted to say that WINDOWTITLE cannot match a suffix, and this makes it useless for matching window titles in a big lot of cases. – wolfrevokcats Mar 05 '18 at 12:05
  • Fine. I went into detail to make it clear that we can't hard code searching for "Administrator: %TITLE%" either, because it uses a localized name for "Administrator". You could switch to using find.exe instead of findstr.exe. It handles Unicode. Then the title can be set to any localized string without forcing ASCII, assuming the batch script is saved as UTF-8 (no BOM) and changes the codepage to 65001 before reading the title string into `TITLE`. – Eryk Sun Mar 05 '18 at 12:09
0

When messing with command prompts, I kept crashing my computer when command prompts got REALLY out of control. I made another command prompt as follows:

Title END
:End
taskkill /FI "WINDOWTITLE ne END" /IM cmd.exe /F /T
Goto :End

This will force close all command prompts that are running, not including this one, and will repeat the command until you close it.

Noobblue
  • 11
  • 4