如何摆脱扩展的`forfiles`变量中的双引号呢? [英] How to get rid of enclosing double-quotes in the expanded `forfiles` variables?

查看:276
本文介绍了如何摆脱扩展的`forfiles`变量中的双引号呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.txtfile2.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屋!

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