Close folder's window with batch file
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
- How to close Windows Explorer from CMD?
- How do I keep Windows Explorer from interfering with deleting a folder?
- How to close Windows Explorer windows with opened folders from a certain drive?
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"
xarlap
Updated on June 04, 2022Comments
-
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 ofTASKKILL /F /FI "WINDOWTITLE eq test" /IM explorer.exe
The message that displayed was
No tasks running with the specified criteria.
Any help?
-
xarlap almost 10 yearsI have tried to put the full path but neither this is correct
-
foxidrive almost 10 yearsThat's not true when I tested it. Use tasklist and filter for
explorer.exe
and then launchexplorer c:\folder
and repeat your taslisk command. You will findtwo
explorer.exe listed with differentPID
-
Mofi almost 10 yearsYes, 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, buttaskkill
failed in any case on my computer. -
foxidrive almost 10 yearsI tested both on Windows 8.1 and I'm aware of extra spaces being needed in some versions of Windows - a bug.
-
Mofi almost 10 yearsI 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 almost 10 yearsYes, it's user configurable in
Explorer options
to use a single window or multiple windows. -
Mofi almost 10 yearsThat'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 almost 10 yearsI 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'stasklist
does not show thewindow title
in verbose mode either. I couldn't seem to get thetest
window to close withtaskkill
in XP. It seems that this entire question hinges on the Windows version being used. -
foxidrive almost 10 yearsIt works in Windows 8.1 but from the answer by Mofi and comments, the Windows version is important to know.