Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file - TASKKILL /FI "WINDOW TITLE ..." not working

Tags:

batch-file

I'm trying to make a batch file which:

  1. launches internet explorer
  2. waits for internet explorer to load a local .html file into it's web browser
  3. waits for a pop-up window to load
  4. closes the explorer window opened in step 1 (without affecting any web pages which may be previously opened)
  5. closes itself

I've got everything except step 4 working with the batch file shown below.

cd "C:\Program Files\internet explorer"
start "CloseMe" iexplore.exe "file://C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files\Stony Mountain Institute Lift Station.html"
TIMEOUT 3 & REM Waits 3
TASKKILL /IM iexplore.exe /FI "WINDOWTITLE eq CloseMe - Internet Explorer"
exit

If I comment out the exit command & skip step 5 I can see what error message I'm getting. This is INFO: No tasks running with the specified criteria.

I ran this command (thanks aschipfl) & it looks like my Window Title is not being set properly as CloseMe.

C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files>tasklist /FI "IMAGENAME eq iexplore.exe" /v

Image Name                     PID Session Name        Session#    Mem Usage Status          User Name                                              CPU Time Window Title
========================= ======== ================ =========== ============ =============== ================================================== ============ ========================================================================
iexplore.exe                 18076 Console                    1     36,300 K Running         STONY-STP\Operator                                      0:00:00 MultiSmart - Internet Explorer
iexplore.exe                 15864 Console                    1     40,448 K Running         STONY-STP\Operator                                      0:00:00 N/A
iexplore.exe                 18072 Console                    1    155,600 K Running         STONY-STP\Operator                                      0:00:06 N/A

C:\ProgramData\Schneider Electric\Citect SCADA 2016\User\1173051_SM_STP\Files>

I'm setting the Window Title as CloseMe in two places.

  1. in the batch file's second line (scroll up)
  2. in the .html file (see below)

    <title>CloseMe</title>  
    <BODY onLoad="popup('http://192.168.0.10/config.htm', 'Stony Mountain Institute Lift Station')"> 
    
    <SCRIPT TYPE="text/javascript">
       function popup(mylink, windowname) {  
          if (! window.focus)return true;  
             var href;
          if (typeof(mylink) == 'string') href=mylink;  
          else href=mylink.href;
          window.open(href, windowname, 'width=800,height=480,scrollbars=no');  
          //self.close(); 
          return false;
       }  
    </SCRIPT> 
    
like image 485
mike_yung Avatar asked Jul 09 '18 19:07

mike_yung


1 Answers

Internet Explorer internally derives its main windows title from the title of the tab that is currently active within it.

So what you will see as Window Title in the tasklist's output, is based on the title of the tab that is currently active in Internet Explorer.

Starting from Internet Explorer 7 which was the first version with tabbed browsing, It is not possible to kill an instance of iexplorer.exe without affecting other tabs. A single iexplorer.exe instance can host multiple windows or tabs so killing that instance will destroy all the tabs or pages it is hosting.

So let's assume that your desired tab with the title of CloseMe happenes to be the active tab, therefor it could be found by taskkill and tasklist, then TASKKILL /IM iexplore.exe /FI "WINDOWTITLE eq CloseMe - Internet Explorer" sends a termination signal to the main window of the iexplore.exe

In default configuration, Internet Explorer always asks if you want to close the current tab or close all tabs, so the user can choose to close the current tab only. But that needs interaction from user and probably not what you have intended in the first place.

The other possibility which is quite common is that Internet Explorer configured to always close all tabs without asking. That is essentially equivalent to killing all instances of Internet Explorer so there is no point in filtering by WINDOWTITLE in taskkill. You could just use TASKKILL /IM iexplore.exe with the same effect but with 100% success rate.

For this to work you need a utility to send the CLOSE message to the desired window to just close that specific windows not the whole process.

Windows does not have a build-in tool for doing so, but if you can use a third party tool then NirCmd is all you need.

nircmdc.exe win child process "iexplore.exe" child class "Frame Tab" close ititle "CloseMe"

Tested on Windows 7 x64 with Internet Explorer 11

like image 150
sst Avatar answered Nov 15 '22 09:11

sst