如何摆脱扩展的`forfiles`变量中的双引号呢? [英] How to get rid of enclosing double-quotes in the expanded `forfiles` variables?
问题描述
forfiles
命令建立多个变量,以前导@
表示,这些变量将有关当前迭代项的数据返回到循环主体.
The forfiles
command establishes several variables, indicated by a leading @
, which return data concerning the currently iterated item to the loop body.
与迭代项目的路径和名称相关的所有变量都返回包含在""
中的值.这些是:@file
,@fname
,@ext
,@path
和@relpath
.
All the variables related to the path and name of the iterated item return the value enclosed in ""
. Those are: @file
, @fname
, @ext
, @path
and @relpath
.
所以:如何摆脱双引号呢?
So: how can you get rid of the enclosing double-quotes?
例如,以下代码返回给定根目录中文本文件的相对路径:
For example, the following code returns relative paths to text files in the given root directory:
forfiles /P "C:\root" /M "*.txt" /C "cmd /C echo @relpath"
假设C:\root
包含两个文件file1.txt
和file2.txt
,则输出为:
Assuming that C:\root
contains two files file1.txt
and file2.txt
, the output will be:
".\file1.txt"
".\file2.txt"
但是,我想要没有周围""
的文件列表.
However, I want the list of files without the surrounding ""
.
我正在使用64位Windows 7.
I am working on Windows 7 64-bit.
推荐答案
一种方法是在forfiles
内嵌套for %I
循环并使用%~I
扩展-在命令提示符"窗口中使用以下代码:
One approach is to nest a for %I
loop within the forfiles
and use the %~I
expansion -- use this code in a Command Prompt window:
forfiles /P "C:\root" /M "*.txt" /C "cmd /Q /C for %I in (@relpath) do echo %~I"
要在批处理文件中使用该代码,必须将%
符号加倍:
To use that code within a batch file you must double the %
-signs:
forfiles /P "C:\root" /M "*.txt" /C "cmd /Q /C for %%I in (@relpath) do echo %%~I"
返回的文件列表将是(依赖于原始问题中的示例文件):
The returned list of files will be (relying on the sample files from the original question):
.\file1.txt
.\file2.txt
另一种变体是将另一个forfiles
嵌套在初始的主体中,因为forfiles
会删除给定字符串中的(不转义的)双引号,例如/C
之后的命令行:
Another variant is to nest another forfiles
in the body of the initial one, because forfiles
removes (non-escaped) double-quotes within given strings like the command line after /C
:
forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\""
或者(故意将内部的forfiles
加倍,这可以解决一个错误-参见此帖子) :
Or alternatively (the doubled inner forfiles
is intentional, this works around a bug -- see this post):
forfiles /P "C:\root" /M "*.txt" /C "forfiles forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\""
内部forfiles
将精确地枚举一项,这是由外部循环传递的一项.由于@relpath
在执行内部循环时已经展开,因此引号将被删除,因为它们不会被转义.
The inner forfiles
will enumerate exactly one item, which is the one passed over by the outer loop. Since @relpath
is already expanded when the inner loop is executed, the quotes are removed as they are not escaped.
所以返回的文件列表看起来像(再次从原始问题中获取示例文件):
So the returned list of files looks like (again taking the sample files from the original question):
.\file1.txt
.\file2.txt
行之间的附加换行符由forfiles
生成.您可以避免使用重定向(关闭forfiles
输出,但仅在控制台窗口中显示echo
输出):
The additional line-break between the lines is generated by forfiles
. You can avoid that using redirection (dismiss forfiles
output, but display only the echo
output in the console window):
> nul forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C 0x22cmd /C > con echo @relpath0x22"
这篇关于如何摆脱扩展的`forfiles`变量中的双引号呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!