3

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
xarlap
  • 157
  • 1
  • 2
  • 14

4 Answers4

4

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

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 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` – foxidrive Jul 23 '14 at 14:51
  • 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. – Mofi Jul 23 '14 at 14:59
  • I tested both on Windows 8.1 and I'm aware of extra spaces being needed in some versions of Windows - a bug. – foxidrive Jul 23 '14 at 15:01
  • 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. – Mofi Jul 23 '14 at 15:16
  • Yes, it's user configurable in `Explorer options` to use a single window or multiple windows. – foxidrive Jul 23 '14 at 15:19
  • 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. – Mofi Jul 23 '14 at 15:26
  • 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 Jul 23 '14 at 15:58
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"
Florian Straub
  • 826
  • 9
  • 18
0

This works in Windows 8.1 with explorer folder options set to single window but from the answer by Mofi and comments in Mofi's answer, then the solution depends on the Windows version.

What works in Windows 8.1 doesn't work in Windows XP.

TASKKILL /F /FI "WINDOWTITLE eq %userprofile%\desktop\test" /IM explorer.exe
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

The error results when you've enabled display full path in title, under Folder View options.

You may replace 'test' with full path, or disable full path display.

By default, explorer runs as single process, and any windows that open are just a thread of the process.
Normally, to close a program, you'd send a close message to the process. In this case, closing explorer.exe will close all explorer windows.

To close individual windows, you'd open each window via it's own process. This can be done via registry setting or enabling under View->Options->View->Advanced Settings: "Launch ... separate process"

a) Find PID (process ID) of window you wanna close.

via taskmanager:
1. In list of processes, click the arrow to the left of "Windows Explorer"
2. Check the window name matches the window you wanna close
3. Right click on "Windows Explorer", click "Go to Details"
4. Record the pid

via CMD:
tasklist /V /FI "IMAGENAME eq explorer.exe"

If each explorer window is open in it's own process, the above command would display the window title in the last column.
Otherwise "N/A" would be displayed.

The pid of all explorer windows would be the same. Explorer.exe processes have their own pid, and title "N/A"
If 'separate process' has been enabled eg. via Folder View option, then each window can be closed via the process id & filter option of taskkill.

To close, the desired window has to be activated first, otherwise closing with pid will close the last active window, or closing with window title filter will give error:

INFO: No tasks running with the specified criteria.

b) taskkill /pid <pid> will close the last active window.
Repeating this command will the next window.

or taskkill /im explorer.exe /fi "windowtitle eq <window name>"
or taskkill /fi "IMAGENAME eq explorer.exe" /fi "windowtitle eq <window name>"

< window name > is not case sensitive
If full path in title bar has been enabled in Folder view, then include full path or wildcards.

To close all explorer windows:
taskkill /im explorer.exe

Notes:

  1. To activate explorer window, issue same command to open the window, if window reusing is enabled.
  2. The pid of explorer window(ing) process is in the last row of the response table, in column "PID"; can be accessed via FOR loop.
  3. A vbs workaround to close window from @HelpingHand:
    https://superuser.com/questions/1263315/how-to-close-a-particular-opened-folder-using-cmd-or-batch-file
  4. A vbs workaround to activate window:
    http://superuser.com/questions/327676/application-to-automatically-switch-between-two-applications-in-windows

Tested on Win 10

Zimba
  • 2,854
  • 18
  • 26