Closing command prompt windows.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Closing command prompt windows.

#1 Post by YukoValis » 20 Feb 2019 11:49

So I've run into a dead end again. After the super amazing AGerman helped me with a batch that opens multiple ping command prompts, I've been a lot better at work. Now I'm seeking to improve it even more.
So I run my batch, and it opens about 8 command prompts of pings. I was wondering if I could put a delay in the script so when you press a button, it would close all those windows.
I got as far as killing them using another batch for testing purposes, but that just makes the ping stop.

Even if the resolution opened a new command prompt, where I could put in the instruction "Press enter to close windows" I'm sure i can figure out the pause and cls and all that.
I'm just not sure what command I'd use to close a bunch of windows I made.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#2 Post by aGerman » 20 Feb 2019 15:46

Sub string "- ping " should be part of all of the window titles which is what you could use to find the right windows.
Insert that line in the batch code where you create the ping windows. It creates another window which is already iconic in your task bar. Whenever you want to close the windows then click on the "Close Pings" icon in your task bar and then press any key

Code: Select all

start "Close Pings" /min cmd /s /q /d /c "pause>nul|set /p "=Press any key to close ping windows ..."&echo(&for /f "tokens=2 delims=," %%i in ('tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh ^| findstr /c:"- ping "') do taskkill /pid %%i"
Steffen

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#3 Post by YukoValis » 20 Feb 2019 16:56

damn. It doesn't seem to be working. Maybe it conflicts with something else I have to make all this work? Here I'll show you what I have.

Code: Select all

@if (@a)==(@b) @end /*
@echo off &setlocal
set "start=cscript //nologo //e:jscript %~fs0 "
set /p number="4 digit? "?
rem Made by Chris K and mostly Steffen
::                                    columns   lines             target               left  top 
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number% -t ^"^""    0    0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%vma -t ^"^""    0  342
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%vmb -t ^"^""    0  684
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& tracert %number% ^"^""  579    0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%sa -t ^"^""  579  342
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%sd -t ^"^""  579  684
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%a -t ^"^""  1158  0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%b -t ^"^""  1158  342


goto :eof */
var objWMIService = GetObject('winmgmts:\\\\.\\root\\CIMV2'),
    objProcess = objWMIService.Get('Win32_Process'),
    objParam = objProcess.Methods_('Create').InParameters.SpawnInstance_(),
    objStartup = objWMIService.Get('Win32_ProcessStartup').SpawnInstance_();
objStartup.X = WScript.Arguments(1);
objStartup.Y = WScript.Arguments(2);
objParam.Properties_.Item('CommandLine').Value = WScript.Arguments(0);
objParam.Properties_.Item('ProcessStartupInformation').Value = objStartup;
objWMIService.ExecMethod('Win32_Process', 'Create', objParam);
It basically opens all the things I need to ping. I'm trying to have it open like a 9th window at 1158 684 with the "Press any key to close" I am perfectly fine with the tracert not closing.
Last edited by YukoValis on 22 Feb 2019 14:38, edited 4 times in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#4 Post by aGerman » 20 Feb 2019 17:02

I don't see any reason why it shouldn't work. Just insert the line after
@echo off &setlocal
and try again.

Steffen

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#5 Post by YukoValis » 20 Feb 2019 17:19

It seems to close just that window, and all the others remain open.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#6 Post by aGerman » 20 Feb 2019 17:27

aGerman wrote:
20 Feb 2019 15:46
Sub string "- ping " should be part of all of the window titles
Is that also true in your environment? That's what FINDSTR is currently looking for. You could try to replace
findstr /c:"- ping "
with
findstr /c:"%number%"
Of course you have to input the value of variable number beforehand in that case.

Steffen

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#7 Post by YukoValis » 20 Feb 2019 17:40

Hmm no. I don't think it is working like that. It only seems to close that window. I've tried to figure a way to alter it like putting a wild card, even if I don't know if those exist in this format. before the variable, after the variable. etc
I hate to say it. but is there a more simple solution? : (
I'm going to be away for two days. So I'm not ignoring this thread. I'm sorry.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#8 Post by aGerman » 21 Feb 2019 10:54

I don't know how to help you because I can't even reproduce your problem. The line works for me.
I also don't know how to simplify this line. As soon as you terminate all cmd.exe processes using
TASKKILL /IM cmd.exe /F
you would possibly terminate the process that calls taskkill.exe before it was able to close all of your ping windows. I guess this results in undefined behavior.

Steffen

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#9 Post by YukoValis » 22 Feb 2019 12:04

and I'm back. Thank you for the help by the way. It might be something in my work that is not allowing the line to work. Since things are a bit restrictive here. I'm surprised I can even access the registry with the original to place the windows. I'll take what you gave me and fiddle about with it. I'll post here if I find anything else about it. : )

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#10 Post by YukoValis » 26 Feb 2019 14:15

I just have a random thought. When batch calls a command window, is it possible for the command to name it something or even assign the ID? In which case there could be a bunch of kill or close commands behind a pause further in the script.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#11 Post by aGerman » 26 Feb 2019 15:07

The Win32_ProcessStartup class has a Title member that you could use. That way (if the specified string is the beginning of the window title) you can use the filter with a wildcard directly. Besides of that there is no big difference.

This works for me. (Line objStartup.Title = 'Observation'; is new in the JScript section.)

Code: Select all

@if (@a)==(@b) @end /*
@echo off &setlocal
set "start=cscript //nologo //e:jscript %~fs0 "

start "Close Observations" /min cmd /s /q /d /c "pause>nul|set /p "=Press any key to close ping windows ..."&echo(&taskkill /f /t /fi "WINDOWTITLE eq Observation*" /im cmd.exe"

%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping localhost  -t ^"^""    0    0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping google.com -t ^"^""    0  342

goto :eof */
var objWMIService = GetObject('winmgmts:\\\\.\\root\\CIMV2'),
    objProcess = objWMIService.Get('Win32_Process'),
    objParam = objProcess.Methods_('Create').InParameters.SpawnInstance_(),
    objStartup = objWMIService.Get('Win32_ProcessStartup').SpawnInstance_();
objStartup.X = WScript.Arguments(1);
objStartup.Y = WScript.Arguments(2);
objStartup.Title = 'Observation';
objParam.Properties_.Item('CommandLine').Value = WScript.Arguments(0);
objParam.Properties_.Item('ProcessStartupInformation').Value = objStartup;
objWMIService.ExecMethod('Win32_Process', 'Create', objParam);
Steffen

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#12 Post by YukoValis » 27 Feb 2019 11:18

Aha I see the problem. So it only closes the tracert, which is the only thing renamed. I'm guessing since the "set /p number="store number? "?" I need is messing with those windows, and not letting the name be changed.
Either that or I'm putting that command in the wrong place. Maybe the command you recently added can be altered to include multiple names? like "WINDOWTITLE eq Observation*", "WINDOWTITLE eq "ping %number% -t", "WINDOWTITLE eq "ping %number%vma -t"*" *" etc etc?

Code: Select all

@if (@a)==(@b) @end /*
@echo off &setlocal
set "start=cscript //nologo //e:jscript %~fs0 "

start "Close Observations" /min cmd /s /q /d /c "pause>nul|set /p "=Press any key to close ping windows ..."&echo(&taskkill /f /t /fi "WINDOWTITLE eq Observation*" /im cmd.exe"
set /p number="store number? "?

::                                    columns   lines             target               left  top 
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number% -t ^"^""    0    0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%vma -t ^"^""    0  342
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%vmb -t ^"^""    0  684
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& tracert %number% ^"^""  579    0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%sa -t ^"^""  579  342
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%sd -t ^"^""  579  684
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%a -t ^"^""  1158  0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number%b -t ^"^""  1158  342


goto :eof */
var objWMIService = GetObject('winmgmts:\\\\.\\root\\CIMV2'),
    objProcess = objWMIService.Get('Win32_Process'),
    objParam = objProcess.Methods_('Create').InParameters.SpawnInstance_(),
    objStartup = objWMIService.Get('Win32_ProcessStartup').SpawnInstance_();
objStartup.X = WScript.Arguments(1);
objStartup.Y = WScript.Arguments(2);
objStartup.Title = 'Observation';
objParam.Properties_.Item('CommandLine').Value = WScript.Arguments(0);
objParam.Properties_.Item('ProcessStartupInformation').Value = objStartup;
objWMIService.ExecMethod('Win32_Process', 'Create', objParam);
Last edited by aGerman on 27 Feb 2019 12:33, edited 1 time in total.
Reason: Please use code formatting.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#13 Post by aGerman » 27 Feb 2019 12:31

As you can imagine I can't try with your host names. So I have no other chance than pinging common sites. And I still didn't get any feedback whether my example works for you if you use it unchanged.

Code: Select all

@if (@a)==(@b) @end /*
@echo off &setlocal
set "start=cscript //nologo //e:jscript %~fs0 "

start "Close Observations" /min cmd /s /q /d /c "pause>nul|set /p "=Press any key to close monitoring windows ..."&echo(&taskkill /f /t /fi "WINDOWTITLE eq Observation*" /im cmd.exe"

set /p "name=what domain? "

%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %name%.de  -t ^"^""    0    0
%start% "cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %name%.com -t ^"^""    0  342

goto :eof */
var objWMIService = GetObject('winmgmts:\\\\.\\root\\CIMV2'),
    objProcess = objWMIService.Get('Win32_Process'),
    objParam = objProcess.Methods_('Create').InParameters.SpawnInstance_(),
    objStartup = objWMIService.Get('Win32_ProcessStartup').SpawnInstance_();
objStartup.X = WScript.Arguments(1);
objStartup.Y = WScript.Arguments(2);
objStartup.Title = 'Observation';
objParam.Properties_.Item('CommandLine').Value = WScript.Arguments(0);
objParam.Properties_.Item('ProcessStartupInformation').Value = objStartup;
objWMIService.ExecMethod('Win32_Process', 'Create', objParam);
So this example has a SET /P command where I enter google. If that still doesn't work then I'm at wit's end.

Steffen

YukoValis
Posts: 22
Joined: 08 Jan 2019 12:43

Re: Closing command prompt windows.

#14 Post by YukoValis » 27 Feb 2019 13:28

Sadly no this one or the one before work. I tried normally, and with mucking about with them. I'm not putting in a domain name for %number% I'm putting in the store number. i.e. 0436, 1767.
Here is the thing I did notice. You did manage to change the tracert window to "Observations" that I feel was a huge step. If there was only a way to make a window have that name in the line.

%start% "Observations"cmd /s /q /d /k ^"^"mode con cols=70 lines=25 ^& ping %number% -t ^"^"" 0 0
or something that would actually work lol. Then the command before would close all of them.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Closing command prompt windows.

#15 Post by aGerman » 27 Feb 2019 16:26

So basically you're saying that your window titles don't begin with the word Observation?
ping.PNG
ping.PNG (41.39 KiB) Viewed 8862 times

Post Reply