Batch file 命令的批处理未返回预期路径
Windows中的Batch file 命令的批处理未返回预期路径,batch-file,Batch File,Windows中的FOR命令有一些在DOS中没有的选项。具体来说,它支持如下返回文件名段 In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax: %~I - expands %I removing any surrounding quotes (") %~fI - ex
FOR
命令有一些在DOS中没有的选项。具体来说,它支持如下返回文件名段
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
当我尝试使用以下命令时,它不起作用。它不打印
\程序文件
,而是打印\
for %i in ("%programfiles%") do echo %~pi
似乎只有
p
参数不正确,因为其他参数返回预期结果
> for %i in ("%programfiles%") do echo %i
C:\Program Files
> for %i in ("%programfiles%") do echo %~i
C:\Program Files
> for %i in ("%programfiles%") do echo %~fi
C:\Program Files
> for %i in ("%programfiles%") do echo %~is
C:\PROGRA~1
> for %i in ("%programfiles%") do echo %~id
C:
有人知道可能是什么问题吗?
%~pI-仅将%I扩展到路径
但它假定%I是路径+文件名,因此它会删除最后一个组件
我用一个文件C:\temp\two\myFile.txt测试了它
对于“%c:\temp\two\myFile.txt”中的%%I,执行echo%~pI
显示\temp\two\
但仅适用于路径
对于%%I in(“c:\temp\two”)执行回显%%pI
它只显示“\temp”
但是如果结尾有一个\的话
对于%%I in(“c:\temp\two”)执行回显%%pI
它再次显示\temp\two\
这不是问题,这是正确的预期结果<代码>~p
提取给定文件规范的路径部分
在这种情况下
是驱动器,由c:
返回李>%~di
是路径,由\
返回李>%~pi
是名称,由程序文件
返回李>%~ni
- 而
是完整的扩展文件规范,由C:\Program Files
或%~fi
返回%~dpnxi
执行echo%~pi 代码>返回\程序文件\ 代码>所以我猜是这样的(虽然这不是我想要的)。