I found several such questions and tried to adapt the answers to my needs but no luck so far. I'm trying to create two context menu entries in Windows 7 that would:

  1. Move all files from subfolders in a given folder to the parent folder. Duplicates should be renamed to Filename-Duplicate but not deleted.
  2. Delete all empty subfolders.

I was able to successfully create the needed entries in:

HKEY_CLASSES_ROOT\Directory\shell\Delete Empty Subfolders\command


HKEY_CLASSES_ROOT\Directory\shell\Move all subfiles here\command

The commands (and I think here lies the problem) are as follows for the two:

for /r ROOT %%d in (*.*) do move "%%d" "%1\"
for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"

When I select a folder that I created for testing this and select either one from the context menu it seems the batch file on disk where the Registry entry points to is found and executed but with no result.

I think the problem is the commands themselves. I'm not very experienced with FOR either. If this can be accomplished with PowerShell I'm open to that option. If at all possible I would avoid using external dependencies such as 3rd party apps to execute the commands.


The problem is not with your command. Instead it is related to the Registry because when you do it like this Windows will start looking for a program called for.exe

HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\for.exe
HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\for.exe

and then it starts looking for your program for.* in System32 and the Windows directories. When this fails it returns an error.

As a beginning try to make a .bat file and put your commands in it. In the Registry try to refer to the full path of your batch file.

P.S: You will have another problem with long file names.

  • That's what im doing already but it's not working but it doesent throw any error either. Just silently fails. Image: i.imgur.com/IjKgmqW.jpg – TMRW Aug 21 '14 at 13:58
  • 1
    remove all the double quotes from the image – M. Abdelhafid Aug 21 '14 at 14:35
  • try to catch the log with Process Monitor and search your command line file and continue tracing – M. Abdelhafid Aug 21 '14 at 14:43
  • Ok. Some progress. Removed double quotes from both registry values as you suggested and tweaked the commands themselves a bit. Now empty folders are removed and it appears to work correctly. The problem was DIR /S flag. As for moving files to parent folder it works but duplicates are overwritten and result is a single copy. And what about those long filenames? – TMRW Aug 22 '14 at 13:41

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.