How to close a particular opened folder using cmd or batch file?

I can open a particular folder using this command:

start "" "c:\foldername"

In the same vein, how can I close the same folder using the Command Prompt or batch?


Solution 1:

By default, Explorer does not spawn separate processes, so you can't kill one process without killing all windows, the taskbar and everything else explorer does.

However, you can simply enable opening explorer windows in their own process and suddenly, you can stop one window from the commandline.

How to change explorer to open a window in its own process?
Getting there is different for every windows version, and since you forgot to mention which version of windows you are using, I will assume Windows 10.

In any explorer window, open the View tab, then at the right, press options.

In the explorer options, go to the tab View, and under Advanced options, look for a setting that says: Launch folder windows in a separate process

This option can also be changed from the registry, but I don't know its exact location.

If you want to be able to kill a window by its path, then you also have to change the following setting: enable Display the full path in the title bar

This second setting is required because we need to express what particular window we want to kill by its title. By default it only shows the name of the current folder.

So if you have a C:\Program Files and a D:\Program Files, it will show Program Files for both, and if you have both folders open, 1 command will kill both windows.

That said, once you have enabled this, you can use taskkill to end that window.

In order to learn what kind of command you would have to enter, you can use tasklist to find the window first. The beauty of tasklist is that it uses mostly the same syntax as taskkill. At least for what we want, its the same.

Finding the window
Open the window you want first, then open a command prompt. Now type

tasklist /V /FI "IMAGENAME eq explorer.exe"

This will tell you exactly what you can kill.

In your case you will want to use the following:

tasklist /V /FI "WINDOWTITLE eq C:\Program Files"

if you want to kill Program Files.

Do note, if you have multiple windows open, the windows name is only shown of the LAST window that was active.

If you want to find any window that has a path open with C:\ in Explorer, you can use the following command:

tasklist /V /FI "IMAGENAME eq explorer.exe" /FI "WINDOWTITLE eq C*"

Notice, I use a * here. tasklist can use a wildcard, but only AFTER the search. So in the above case, the windows title still has to start with a C, but anything else is optional.

Now, once you found out what the command is that you want to use to kill the window, replace tasklist with taskkill, and remove the /V parameter. The /V is only used to show more information, such as the windows title.

So to kill the last opened explorer window that starts with a C, you can use:

taskkill /FI "IMAGENAME eq explorer.exe" /FI "WINDOWTITLE eq C*"

Solution 2:

This is a PowerShell script that loops through all Explorer windows and close if it matches the folder path without any aggressive methods like taskkill or Alt+F4

$folder = [uri]'C:\path\to\folder'
foreach ($w in (New-Object -ComObject Shell.Application).Windows()) {
    if ($w.LocationURL -ieq $folder.AbsoluteUri) { $w.Quit(); break }
}

Alternatively:

foreach ($w in (New-Object -ComObject Shell.Application).Windows()) {
    if ($w.Document.Folder.Self.Path -ieq $folder) { $w.Quit(); break }
}

This can also be done in Jscript, VBS or any languages that supports Scriptable Shell Objects. This is a hybrid batch-jscript that does the same thing. The path needs to be in file URI scheme instead of a normal path, so C:\foldername would become file:///C:/foldername

@if (@CodeSection == @Batch) @then
@echo off
cscript //e:jscript //nologo "%~f0" %*
exit /b
@end

// JScript Section
var folder = "file:///C:/foldername"; // the folder to check
var shellWindows = (new ActiveXObject("shell.application")).Windows();
if (shellWindows != null)
{
    for (var win = new Enumerator(shellWindows); !win.atEnd(); win.moveNext())
        if (folder == win.item().LocationUrl) win.item().Quit()
}