Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 复制文件';上下文菜单中的父目录路径_Batch File_Cmd - Fatal编程技术网

Batch file 复制文件';上下文菜单中的父目录路径

Batch file 复制文件';上下文菜单中的父目录路径,batch-file,cmd,Batch File,Cmd,我正在学习批处理脚本,因为它对于设置Windows用户选择的一些快速自定义上下文菜单选项以获取文件及其父目录路径非常有用 现在我知道了以下命令,可以将传递的参数作为文件路径,并将其复制到剪贴板: cmd/c(echo.| set/p=“%1”)|剪辑 但是,当在上下文菜单中设置时,为什么下面的语法不能按预期工作呢 cmd/c(echo.| set/p=“%~dp1”)|剪辑 这是一个可变的扩展问题吗?请指导它为什么不工作,以及如何解决它,使其正确扩展 注册表项示例,其中我永久设置要运行的命令

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

现在我知道了以下命令,可以将传递的参数作为文件路径,并将其复制到剪贴板:

cmd/c(echo.| set/p=“%1”)|剪辑
但是,当在上下文菜单中设置时,为什么下面的语法不能按预期工作呢

cmd/c(echo.| set/p=“%~dp1”)|剪辑
这是一个可变的扩展问题吗?请指导它为什么不工作,以及如何解决它,使其正确扩展

注册表项示例,其中我永久设置要运行的命令,带有开关、变量扩展等

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)在命令提示符或批处理文件中使用的变量相似,但它们之间没有任何关联

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

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

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

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\""
Windows注册表编辑器5.00版
[HKEY\U CURRENT\U USER\Software\Classes\*\shell\Copy Path to clipboard\Command]

@=“cmd.exe/e:on/d/s/c\”代表%%a in(\%1\”)do@(设置/p\“=%%~dpa\”
%W
,当您右键单击文件时,它通常包含工作目录,将是文件的父目录

因此,您可以尝试以下方法:

Windows注册表编辑器5.00版
[HKEY\U CURRENT\U USER\Software\Classes\*\shell\HoldParent]
@=“剪贴板的父级”
[HKEY\U CURRENT\U USER\Software\Classes\*\shell\HoldParent\command]
@=“Cmd/C\”回显%W |剪辑”
对于潜在的unicode/字符问题,您可以对其进行一些扩展,但通常情况下,它的性能应符合要求,而不必担心

编辑

稍微扩展一下(包括不换行),也许:

Windows注册表编辑器5.00版
[HKEY\U CURRENT\U USER\Software\Classes\*\shell\HoldParent]
@=“剪贴板的父级”
[HKEY\U CURRENT\U USER\Software\Classes\*\shell\HoldParent\command]
@=“Cmd/Q/D/U/C\”设置/P\“=%W\”
将
%1
替换为
%w
。2个注册表项以演示
%1
作为
复制文件路径
%w
作为
复制文件父路径

从页面底部的引用

%0或%1是第一个文件参数。例如“C:\Users\Eric\Desktop\New Text Document.txt”。一般来说,这应该用引号括起来,应用程序命令行解析应该接受引号,以消除名称中带有空格和不同命令行参数的文件的歧义(这是一种安全最佳实践,我相信MSDN中提到过)。 %N(其中N为2-9),替换为第N个参数 %*替换为所有参数 %~替换为以第二个参数开头和后面的所有参数 %d第一个参数的绝对解析名称(对于没有文件系统路径的项) %h热键值 %i IDList存储在共享内存句柄中,并在此处传递。 %l第一个参数的长文件名形式。注意:win32应用程序将被传递长文件名,而win16应用程序将被传递短文件名。最好指定%L,因为这样可以避免探测应用程序类型。 %s show命令 %v对于none表示all,如果没有传递参数,则这是工作目录 %w工作目录
尝试使用
%~dp0
而不是
%~dp1
,因为命令的语法是
%~dpI
,而
I
是变量索引的占位符
0
=调用文件,
1
=参数1,
2
=参数2,等等。。。这里有更好的解释:我需要参数#1(filepath)文件的目录路径,而不是调用文件。为什么要使用两组双引号?这是一种转义引号的方法,特别是在注册表“command”键中传递命令时,该键存储命令字符串,如
@=“”
。请在您的问题中提供一个代码示例。这是一个很好的示例,有很好的解释,我到目前为止还不知道%w。它工作得非常正确,甚至给出了不带引号的路径,这对我来说很有魅力,而且非常简单。谢谢。
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\""
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\*\shell\Copy Files Path\command]
@="cmd /c (echo. | set /p =\"\"%1\"\") | clip"

[HKEY_CURRENT_USER\Software\Classes\*\shell\Copy Files Parent Path\command]
@="cmd /c (echo. | set /p =\"\"%w\"\") | clip"
%0 or %1 the first file parameter. For example “C:\Users\Eric\Desktop\New Text Document.txt”. Generally this should be in quotes and the applications command line parsing should accept quotes to disambiguate files with spaces in the name and different command line parameters (this is a security best practice and I believe mentioned in MSDN). %N (where N is 2 - 9), replace with the nth parameter %* replace with all parameters %~ replace with all parameters starting with and following the second parameter %d desktop absolute parsing name of the first parameter (for items that don’t have file system paths) %h hotkey value %i IDList stored in a shared memory handle is passed here. %l long file name form of the first parameter. Note win32 applications will be passed the long file name, win16 applications get the short file name. Specifying %L is preferred as it avoids the need to probe for the application type. %s show command %v for verbs that are none implies all, if there is no parameter passed this is the working directory %w the working directory