I am learning batch scripting since it's very useful for setting some quick custom context menu options of Windows user's choice to get file and it's parent directory's path.
Now I know following command to get the passed argument as file path and copy it to clipboard:
cmd /c (echo.|set /p=""%1"") | clip
But then why isn't following syntax working as expected when set in Context menu ?
cmd /c (echo.|set /p=""%~dp1"") | clip
Is it a variable expansion issue ? Please guide as to why it isn't working and how to resolve it so that it expands properly.
Sample of registry entry wherein I am permanently setting the command to be run, with switches, variable expansions etc.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\*\shell\Copy File's Parent Path]
@="Copy File's Parent Path"
[HKEY_CURRENT_USER\Software\Classes\*\shell\Copy File's Parent Path\Command]
@="cmd.exe /c (echo.|set /p=\"\"%~dp1\"\") | clip"
Variables like
%1
,%*
,%V
in registry, are placeholders which will be resolve and replaced by Windows Shell component (Win32 Shell). They just happened to be similar to the variables that is used by Windows Command Processor(CMD.EXE) at Command Prompt or in batch files, But they are in no way related to each other.For instance with CMD.EXE
%1
can only be used within batch files it has no meaning at Command Prompt or when passed as a command by/C
or/K
switch. In your registry sample%1
will be resolved by Windows Shell not by CMD.EXE so you can not use things like%~dp0
or%~dp1
in registry, They have no meaning to Windows Shell.So you have to take
%1
by CMD.EXE (which have been automatically replaced by its actual value) and do processing with it in aFOR
loop to obtain the directory path of the file.This is the actual command that will extract path information from file name:
It can be entered as is, in Registry Editor to the
(default)
value of the appropriate Key e.g:HKEY_CURRENT_USER\Software\Classes\*\shell\Copy Path to clipboard\Command
for current user orHKEY_CLASSES_ROOT\*\shell\Copy Path to clipboard\Command
for all users.And the string for registry script:
A sample
.REG
script will look like this:When you want to pass a string that contains percents(
%
) you have to escape the percents by doubling them, pretty much the same as you do in batch files.Replace
%1
with%w
. 2 registry entries to demonstrate%1
asCopy Files Path
and%w
asCopy Files Parent Path
.Reference from Extending Shortcut Menus at the bottom of the page.
%W
, which generally holds the working directory, when you right click on a file, would be the files' parent.You could therefore try this:
You could extend this a little for potential unicode/character issues, but in general it should perfom as required without.
Edit
To extend it a little, (including no newline), perhaps: