Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close Windows Explorer from CMD

Is it possible to close Windows Explorer from CMD? I have a batch that does this: it will change directory, open explorer in this folder, than run a program. After the user closes the program the batch should close the explorer (or all explorers opened), continue on next folder (cd folder), run the same program in this folder and so on. Till the last folder is processed.

like image 418
John Boe Avatar asked Apr 07 '12 18:04

John Boe


2 Answers

Close the explorer windows by killing the explorer process (note that this may do more than just kill the windows, but it will definitely do that):

for example, use win+r and try this

cmd /c "taskkill /f /im explorer.exe && start explorer"

If you kill explorer without restarting it, use Ctrl+shift+Esc to pull up the taskmanager and start a new task "explorer".

like image 191
user3331639 Avatar answered Sep 27 '22 21:09

user3331639


Not from a batch file unless you want to write your own command line application that opens up a windows explorer window, and (this is the key part) somehow knows the window handle of that explorer window, so it can post a WM_CLOSE message to it, which basically simulates someone closing that window.

How you would determine "all the explorer windows that got opened" would be that instead of just starting explorer.exe instances from a command line you would do it from your own application.

I think that determining the window handle (HWND in win32 api terms) and posting a close message would be better than trying to track process handles and terminating explorer process instances, since that could cause some side effects beyond those that you'd want.

like image 23
Warren P Avatar answered Sep 27 '22 20:09

Warren P