4

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?

  • but, am creating a small batch file for my visitors. So, there is no guarantee that all will have installed AutoHotKey. So, understand my situation and provide me a solution pls. – Philip Oct 28 '17 at 17:28
  • 5
    What about the VBS one liner: set shell = createobject("wscript.shell") : if shell.appactivate("foldername") then shell.sendkeys "%{F4}" You could echo that out from a batch file and run it if it has to be a batch file. – HelpingHand Oct 28 '17 at 18:15
  • 1
    Awesome That works good when use that vbs in batch using echo. Thanks :) @Helping Hand – Philip Oct 29 '17 at 6:15
4

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*"
| improve this answer | |
  • Excellent and thorough answer. I had specifically not suggested the OP kill Explorer knowing it would kill more than the desired window. Totally forgot about being able to open in separate processes. – I say Reinstate Monica Oct 28 '17 at 19:53
  • In the latest update of Windows BuildNumber: 18363 when I open up a folder location and do the tasklist /V /FI "IMAGENAME eq explorer.exe" command I get NA in the Window Title column. – Ste Jan 18 at 10:29
  • @StephenSherry I just tested it on my end, it seems Microsoft did indeed change something. I no longer get all the windows, only the one I last clicked will appear in such search, even though I have enabled open in separate process. EDIT: I appear to be using an older build even though my pc is up-to-date. Are you using an insider build by any chance? I'm, using 10.0.17763.973 – LPChip Jan 18 at 10:45
  • @StephenSherry one other thing. I also have ActualTools Window Manager installed, in which I can enable grouping explorer windows together to 1. If I enable this, I no longer can do this either and I get an N/A too. However, there is a new process then that shows me the window title. In my case ActualToolsTabbedContaine. I know that Windows will get the same functionality, grouping windows together. I'm pretty sure the same effect is happening to you. So if you do tasklist /v you can see what process now holds the correct window. Killing that will kill all file managers though. – LPChip Jan 18 at 10:53
  • My update was pushed last night so yours will be push soon as they're regional AFAIK. Gonna have to find another solution because this bug won't be fixed any time soon I would say. – Ste Jan 18 at 11:03
0

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:
    How to close a particular opened folder using cmd or batch file?
  4. A vbs workaround to activate window:
    Application to automatically switch between two applications in Windows

Tested on Win 10

| improve this answer | |
0

This is a hybrid batch-jscript that loops through all Explorer windows and close if it matches the folder path without any aggressive methods like taskkill or Alt+F4. It can also be done in VBS, either alone or in a hybrid batch-VBS file

@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()
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.