3

I am trying to add a new option to the context menu for folders in Windows. I have managed to add the option and specify its command as follows:

xcopy.exe "%0\*" "c:\Destination\" /EHY

This code is added to regedit.exe

Snapshot here.

I have a folder in the c: drive named Destination. I am trying to copy the folder that I right clicked to the Destination folder, without a command prompt window.

What is happening: xcopy is running and copying the content of the folder and in the foreground. Please help me with these two issues:

  1. Run the xcopy command without showing a window.
  2. Copy the folder to a new folder in Destination named after the copied folder.

Thank you.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Hi Bee
  • 31
  • 4
  • 3
    You are aware that your command cannot work as you have stated because you are supposed to use %L, %1 or %~1 not %0. – Compo Apr 01 '17 at 18:01

1 Answers1

1

The command that satisfies the two issues listed is at the very end. First, some notes of explanation.

When you add a shell command to the Windows Registry, you have several variables available to you (such as %1, %L, and %V). Now, you would like a new folder in Destination named after the copied folder. Parameter extensions (such as %~n1) can strip everything from the full path and give you the name of the directory leaf. However, these are not available when using the shell command from the Windows Registry. The most straightforward way to get a plain directory name is to create a temporary batch script, run it, and delete the batch script afterwards.

The following will copy the selected directory as a sub-directory inside Destination:

cmd.exe /c echo @echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & call "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"

This next part requires the use of a third-party utility.

The previous command will open a command window and leave it open as long as copying is in progress. To hide that window, use the tiny utility RunHiddenConsole

The following will copy the selected directory and hide the command window while copying:

"C:\Destination\RunHiddenConsole.exe" cmd.exe /c echo @echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & "C:\Destination\RunHiddenConsole.exe" /w "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"

This could certainly be made more flexible and efficient, but the above command at least demonstrates the technique for accomplishing the task.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49