Close folder's window with batch file

13,686

Solution 1

It is not possible to close the Windows Explorer window of a folder using command taskkill if in Windows registry under

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced

the double word SeparateProcess has the value 0 respectively option Launch folder windows in a separate process on View tab of the Folder Options is not checked.

And as foxidrive found out (read comments below) and I can also confirm, enabling this setting has on Windows XP SP3 no effect on number of explorer.exe running on opening folders even after a restart of Windows.

Therefore it depends on version of Windows (2000, XP, Vista, 7, 8, 8.1) and the Folder Options

  • Display the full path in the title bar
  • Launch folder windows in a separate process

if it is possible at all to use the command taskkill to close an Explorer folder window and which string to find in title bar, just the name of the folder or the full path of the folder.

On Windows XP or with Launch folder windows in a separate process not being enabled the folder window is not opened as separate task respectively as separate process.

Independent on how many folder windows are opened, the number of explorer.exe processes for each user is always just 1. Every folder window is in real just a window of always running Windows Explorer (desktop) opened in a separate thread. Windows Explorer just pretends that the folder window is a separate task on Windows task bar and on Applications tab of the Windows task manager. But only one explorer.exe is listed on Processes tab of Windows task manager even with multiple folder windows opened.

This behavior of Windows Explorer can be better watched with free tool Process Explorer of Sysinternals by selecting explorer.exe and viewing in lower pane on the handles of this process containing among lots of other handles also the handles of the opened folder windows.

A console application is required which finds a window by title, get the handle of this window and sends the WM_CLOSE event message to this folder window. See for example

Solution 2

The solution I found here worked well on XP SP 3. All you need is the small tool NirCmd:

nircmd.exe win close title "some window title"
Share:
13,686
xarlap
Author by

xarlap

Updated on June 04, 2022

Comments

  • xarlap
    xarlap almost 2 years

    I have a folder on my Desktop named test. I want to create a batch file in order to close the window automatically when this folder is opened with Windows Explorer. I tried the next command, but nothing happened on execution of

    TASKKILL /F /FI "WINDOWTITLE eq test" /IM explorer.exe
    

    The message that displayed was

    No tasks running with the specified criteria.
    

    Any help?

  • xarlap
    xarlap almost 10 years
    I have tried to put the full path but neither this is correct
  • foxidrive
    foxidrive almost 10 years
    That's not true when I tested it. Use tasklist and filter for explorer.exe and then launch explorer c:\folder and repeat your taslisk command. You will find two explorer.exe listed with different PID
  • Mofi
    Mofi almost 10 years
    Yes, I saw your comment and tried also the posted command. But it did not work on Windows XP SP3. It looks like it depends on Windows version or a setting if a new instance of explorer.exe is started on opening a new folder window or the folder window is opened in a thread of already running instance of Explorer. I have searched already in WWW for an explanation of the different behavior of Explorer, but could not find anything up to now. There is a setting to control what is displayed in title bar - complete path or just name of folder, but taskkill failed in any case on my computer.
  • foxidrive
    foxidrive almost 10 years
    I tested both on Windows 8.1 and I'm aware of extra spaces being needed in some versions of Windows - a bug.
  • Mofi
    Mofi almost 10 years
    I found the registry setting which controls single/multiple instance behavior of Windows Explorer for each Explorer window and edited my answer with this information. A modification of the registry value becomes effective after log off/log on, or a restart of Windows, or kill and restart of running desktop explorer.exe.
  • foxidrive
    foxidrive almost 10 years
    Yes, it's user configurable in Explorer options to use a single window or multiple windows.
  • Mofi
    Mofi almost 10 years
    That's right. Even on Windows XP there is the option Launch folder windows in a separate process on tab View in Folder Options opened for example from menu Tools of Windows Explorer or from Control Panel. But a restart of Windows is nevertheless needed after enabling that option to become effective.
  • foxidrive
    foxidrive almost 10 years
    I tested it on XP SP3 and enabling the setting for separate windows worked without a reboot, but even after a reboot and opening multiple windows there was just one explorer.exe listed and XP Pro's tasklist does not show the window title in verbose mode either. I couldn't seem to get the test window to close with taskkill in XP. It seems that this entire question hinges on the Windows version being used.
  • foxidrive
    foxidrive almost 10 years
    It works in Windows 8.1 but from the answer by Mofi and comments, the Windows version is important to know.