[batch] close explorer window

Discussion in 'Scripting' started by thethingy, Apr 24, 2011.

  1. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    so I have a batch file along with dependances in a folder, you open the folder to run the batch so how do I get the batch file to close the folder whilst keeping the batch window active?
     
  2. zahnoo

    zahnoo MDL Senior Member

    Feb 2, 2011
    387
    35
    10
    #2 zahnoo, Apr 24, 2011
    Last edited: Apr 24, 2011
    Running it from a shortcut or the Task Scheduler won't open the folder itself. You can change properties of the shortcut so the DOS box/command prompt window will be minimized.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #3 Compo, Apr 25, 2011
    Last edited by a moderator: Apr 20, 2017
    You don't!

    I know it sounds harsh, but why can you not be bothered to click your cursor on the cross in the top right corner of the explorer window?

    The trouble is that unless your user has specifically chosen to change Windows default setting to 'Launch folder windows in a separate process' then you can only close the entire explorer.exe process which as you can understand is a little too excessive. Third party utilities, WMI, your Task Manager and even Powershell cannot help you unless the user has made that change.

    Your only alternative would lie in invoking your batch file via a non windows explorer method. i.e. the two methods suggested above or possibly just typing it directly into the Start Menu run/search box.
    Code:
    "D:\PathTo\My Batch.cmd"
     
  4. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #4 thethingy, Apr 26, 2011
    Last edited by a moderator: Apr 20, 2017
    (OP)
    what it is, is that the batch first runs then extracts about 100 different files from an archive then it creates about 20 files, it's just so the end user misses the mess it creates cause no cleanup is performed till the batch is closed, I could pack the batch but then my batch becomes an instant "trojan", I'm thinking the process can just be ended actually but I dont know how to determine what explorer.exe is what.
     
  5. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #5 Compo, Apr 26, 2011
    Last edited by a moderator: Apr 20, 2017
    The best idea therefore would be to ensure that the extracted files are placed either in a single directory within that location or to use an alternative location. Another solution may be to find a method of executing a Minimize Inactive Windows command (WinKey+Home).

    The problem even with having multiple explorer processes is that you cannot pre-determine which were running prior to the explorer window being opened, (due to the fact that you've already opened it before running the batch). The ProcessIDs are not created in an order and invoked times are not registered so there is no way of isolating the most recent. The only method which may be able to be utilised is through the 'known' window title, although it's not always a certainty.
    In a 'prompt' window, take a look at the output of:
    Code:
    TASKLIST /V /FI "IMAGENAME eq EXPLORER.EXE"
    when your explorer folder is open. If the WINDOWTITLE is provided then you can use it at the beginning of your batch file with the TASKKILL command.
    Code:
    TASKKILL /F /FI "WINDOWTITLE eq %VAR%" /IM explorer.exe
    %VAR% can be tailored accordingly from either %~dp0 or %~p0 depending upon the output noted in your previous TASKLIST command.