Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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 - Fatal编程技术网

Batch file 命令的批处理未返回预期路径

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

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        - 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
    返回

Doh!我想这是有道理的,因为它无法区分文件名和文件夹名,所以它认为程序文件就是文件名。谢谢你的澄清。对于记录,使用(“%programfiles%\”)中的%i的
执行echo%~pi 返回
\程序文件\ 所以我猜是这样的(虽然这不是我想要的)。