7

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.

John Boe
  • 3,501
  • 10
  • 37
  • 71
  • why are you opening explorer in the first place ? – Yahia Apr 07 '12 at 18:49
  • just out of curiosity, why do you need the explorer open when to "run a program". Regardless you could try to find the process id of the explorer you just openened and then end this process. – Cilvic Apr 07 '12 at 18:49
  • The user wants to do some job in the program. The program generates some files and it is good to check if the files are OK. So he should check and open the files to view them. Than he can close the program and continue the work. Sadly it is not possible to run the program to automate whole the job. The process of switching directory is to simplify the job for user. He could simply do a mistake, but this way he cannot do mistake. I do not know how to close a process in cmd. How to find the new process id? – John Boe Apr 07 '12 at 18:57
  • You could build a series of small helper programs in Python, or any other capable language. Think of them as new commands useful in your batch files. – Warren P Apr 07 '12 at 19:11
  • Possible duplicate of [Close folder's window with batch file](http://stackoverflow.com/questions/24911112/close-folders-window-with-batch-file) – Florian Straub May 22 '16 at 07:08

2 Answers2

10

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".

user3331639
  • 109
  • 1
  • 4
1

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.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • I don't know what is it window handle. "unless you want to write your own command line application that opens up a windows explorer window" - Yes it would be ideal. But I know nothing about these things. – John Boe Apr 07 '12 at 19:06
  • This is a programming questions website. Your question is on topic, if you pick a programming language. But try something yourself first. Lots of people could help you if you asked about how to open an explorer window and find or remember the handle for the window that got opened, either from Python, C++, C#, Delphi, or Ruby. Which one do you know or want to learn? – Warren P Apr 07 '12 at 19:08
  • Forget about that. I thought CMD could do that job. – John Boe Apr 07 '12 at 19:39