Registry command for context menu item to copy folder name

2

I often need to quickly copy the name of a folder in Windows 7, and so am trying to create an equivalent to the Copy as path item that appears in Extended context menus (i.e. when doing Shift+Right Click) that will copy only the name of the selected folder to the clipboard.

I've created the Registry key at:

HKEY_CLASSES_ROOT\Directory\shell\Copy folder name\command

...where all of my similar context-menu additions for folders are, and it can be seen here:

enter image description here

However, I've so far been unable to get the code that needs to be executed by the key working as it should. I've come across this solution to grab the current folder of a directory, and this one to pipe the directory name to the clipboard, and put them together to get the following:

for %* in (.) do set FolderName=%~nx* && echo %FolderName%| clip

This code works exactly as expected in the command line.

Prepending cmd /c to it, which is necessary to run a CMD instance from the Registry, gives the following:

cmd /c for %* in (.) do set FolderName=%~nx* && echo %FolderName%| clip

enter image description here

However, placing this in the value data of the Default string value in the \Copy folder name\command key, and then right-clicking a folder and clicking on Copy folder name fails to work.

Wrapping it in quotes also does nothing:

cmd /c "for %* in (.) do set FolderName=%~nx* && echo %FolderName%| clip"

What am I missing here?

Hashim

Posted 2018-04-10T22:14:47.630

Reputation: 6 967

1@PimpJuiceIT Escaping the pipe does nothing unfortunately, neither does getting rid of the last or first quote. – Hashim – 2018-04-10T23:04:38.020

How about cmd /V:ON /c "for %* in (.) do set FolderName=%~nx* && echo !FolderName!|clip" – Pimp Juice IT – 2018-04-10T23:28:45.893

If it works from a BAT file you can call that from your context entry like: "C:\\folderToClip.bat \"%1\" – HackSlash – 2018-04-10T23:31:57.493

@PimpJuiceIT Yeah, I'd already tried enabling and disabling both Delayed Variable Expansion and Command Extensions, no such luck either way. I also used tried using /k instead of /c in the hope that the command window would remain open for debugging purposes, and ditto for pause - no such luck. – Hashim – 2018-04-10T23:38:40.787

@HackSlash It's a last resort, but I'd really rather get such a short command done without any external dependencies. – Hashim – 2018-04-10T23:39:37.007

Okay, I assume you tried "FolderName=%~nx*" quoting the variable that way too... i.e. cmd /V:ON /c "for %* in (.) do set "FolderName=%~nx*" && echo !FolderName!|clip"? I noticed this way trims off the trailing white space too. – Pimp Juice IT – 2018-04-10T23:44:36.327

No answers