Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 'Delete empty folders' to Windows context menu

I'd like to add to context menu this nice shell script that automatically deletes all empty folders and subfolders under the folder it is run from:

for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

To add such an item to (directories) context menu I should run a file with reg extension having a content similar to

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders]

[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders\Command]
@=for /f "usebackq delims=" %%d in (`"dir %1 /ad/b/s | sort /R"`) do rd "%%d"

but this one does not work. How should it be fixed to properly run the one liner shell script?

like image 296
mmj Avatar asked Dec 01 '15 10:12

mmj


Video Answer


1 Answers

Thanks to Joey comment I was able to find the following solution.

Step 1: create a file with reg extension (like for example add_delete_empty_folders_to_context_menu.reg) and put the following lines inside it:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders]

[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders\Command]
@="cmd /c for /f \"usebackq delims=\" %%d in (`\"dir \"%1\" /ad/b/s | sort /R\"`) do rd \"%%d\""

Step 2: double click on your reg file and give all the needed confirmations.

Now you have a handy Delete empty folders command in your context menu, popping up when you right-click on a directory. This works for me on Windows 7 and Windows 10.

Avoid abuse of this functionality, some softwares need their empty folders to work properly.

Follow up

If you experience trouble (as Enora, see below) you can try to use the following line in the reg file, in place of the original one:

@="cmd.exe /K for /f \"usebackq delims=\" %%d in (`\"dir \"%V\" /ad/b/s | sort /R\"`) do rd \"%%~d\""

I actually don't know if this version is more robust, I just report here the solution found by Enora for her case.

like image 93
mmj Avatar answered Oct 11 '22 11:10

mmj