从上下文菜单复制文件的父目录路径 [英] Copy File's Parent directory path from context menu

查看:135
本文介绍了从上下文菜单复制文件的父目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习批处理脚本,因为它对于设置Windows用户选择的获取文件及其父目录路径的快速自定义上下文菜单选项非常有用.

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"

推荐答案

注册表中的%1%*%V之类的变量是占位符,将被Windows Shell组件(Win32 Shell)解析并替换.它们只是碰巧类似于Windows命令处理器(CMD.EXE)在命令提示符处或批处理文件中使用的变量,但是它们之间没有任何关联.

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.

例如,带有CMD.EXE的%1仅可用于批处理文件,在命令提示符下或由/C/K开关作为命令传递时没有任何意义.在您的注册表示例中,%1将由Windows Shell而不是CMD.EXE解析,因此您不能在注册表中使用%~dp0%~dp1之类的东西,它们对Windows Shell没有任何意义.

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.

因此,您必须使用CMD.EXE(已被其实际值自动替换)作为%1,并在FOR循环中对其进行处理以获得文件的目录路径.

So you have to take %1 by CMD.EXE (which have been automatically replaced by its actual value) and do processing with it in a FOR loop to obtain the directory path of the file.

这是实际的命令,将从文件名中提取路径信息:

This is the actual command that will extract path information from file name:

cmd.exe /e:on /d /s /c "for %%a in ("%1") do @(set /p "=%%~dpa")<nul | clip"

可以在注册表编辑器中按原样输入相应密钥的(default)值,例如:对于当前用户,为HKEY_CURRENT_USER\Software\Classes\*\shell\Copy Path to clipboard\Command,对于所有用户,为HKEY_CLASSES_ROOT\*\shell\Copy Path to clipboard\Command.

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 or HKEY_CLASSES_ROOT\*\shell\Copy Path to clipboard\Command for all users.

以及注册表脚本的字符串:

And the string for registry script:

@="cmd.exe /e:on /d /s /c \"for %%a in (\"%1\") do @(set /p \"=%%~dpa\")<nul | clip\""

示例.REG脚本将如下所示:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\*\shell\Copy Path to clipboard\Command]
@="cmd.exe /e:on /d /s /c \"for %%a in (\"%1\") do @(set /p \"=%%~dpa\")<nul | clip\""

当您要传递包含百分比(%)的字符串时,必须通过将它们加倍来对百分比进行转义,这与批处理文件中的操作几乎相同.

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.

这篇关于从上下文菜单复制文件的父目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆