Page 1 of 2

Closing command prompt windows.

Posted: 20 Feb 2019 11:49
by YukoValis
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.

Re: Closing command prompt windows.

Posted: 20 Feb 2019 15:46
by aGerman
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

Re: Closing command prompt windows.

Posted: 20 Feb 2019 16:56
by YukoValis
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.

Re: Closing command prompt windows.

Posted: 20 Feb 2019 17:02
by aGerman
I don't see any reason why it shouldn't work. Just insert the line after
@echo off &setlocal
and try again.

Steffen

Re: Closing command prompt windows.

Posted: 20 Feb 2019 17:19
by YukoValis
It seems to close just that window, and all the others remain open.

Re: Closing command prompt windows.

Posted: 20 Feb 2019 17:27
by aGerman
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

Re: Closing command prompt windows.

Posted: 20 Feb 2019 17:40
by YukoValis
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.

Re: Closing command prompt windows.

Posted: 21 Feb 2019 10:54
by aGerman
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

Re: Closing command prompt windows.

Posted: 22 Feb 2019 12:04
by YukoValis
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. : )

Re: Closing command prompt windows.

Posted: 26 Feb 2019 14:15
by YukoValis
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.

Re: Closing command prompt windows.

Posted: 26 Feb 2019 15:07
by aGerman
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

Re: Closing command prompt windows.

Posted: 27 Feb 2019 11:18
by YukoValis
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);

Re: Closing command prompt windows.

Posted: 27 Feb 2019 12:31
by aGerman
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

Re: Closing command prompt windows.

Posted: 27 Feb 2019 13:28
by YukoValis
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.

Re: Closing command prompt windows.

Posted: 27 Feb 2019 16:26
by aGerman
So basically you're saying that your window titles don't begin with the word Observation?
ping.PNG
ping.PNG (41.39 KiB) Viewed 10380 times