0

I saw this nice cmd command on this SU answer that removes empty folders in a one liner. I thought this would be very useful to have as a right click context-menu option for folders.

So I went over to "Computer\HKEY_CLASSES_ROOT\Directory\Background\shell" on the registry and added a key with a name and another command key underneath that with default value set to cmd.exe /s /c "for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"".

I see the contextmenu option when I right click a folder, and when I click on the menu option, no cmd window opens and Explorer.exe restarts. The command works fine when I manually execute it in cmd though.

Why does this command not work when hit from a folder right-click contextmenu while it does work on it's own? It especially doesn't make sense to me that Explorer.exe crashes as a result of this.

5
  • You aren't actually asking a question, but rather stating a fact, though I can guess the question you wanted to ask(but didn't ask) is:How to make the command run in shell script? It can take multiple steps, but first, try to change cmd /s /c to cmd /k Commented Jan 11, 2021 at 14:22
  • @XeнεiΞэnвϵς yeah indeed I forgot to add a question. I added it. You mention shell script, does this actually run in shell script? I didn't quite understand why the registry key has shell in it. Anyways I tried changing cmd /s /c to cmd /k. It still doesn't work and no CMD window pops up at all. I don't know how to check if the cmd process actually starts or not.
    – DFSFOT
    Commented Jan 11, 2021 at 15:45
  • You can try to check it by using taskmgr, and I am in China am it is late now, so I am going to sleep soon. Commented Jan 11, 2021 at 15:49
  • I'm not that batch literate, having formerly used .vsb files to automate tasks and then transitioning to PowerShell, so I'm not sure how %d functions here. But in terms of context menu commands, the Shell is tries to expand its variables before passing the arguement to cmd.exe. If you test a simple context command, powershell -NoExit -Command "'%d' | echo", under both Directory\Background\Shell and Directory\Shell, you'll see that the Background version errors out. Examing existing Directory\Background entries shows %V correctly expands to the folder path. Commented Jan 11, 2021 at 20:39
  • @KeithMiller I tried %%d instead of %d and it worked. Thanks!
    – DFSFOT
    Commented Jan 13, 2021 at 11:03

0

You must log in to answer this question.