简体   繁体   English

如何摆脱扩展的`forfiles`变量中的双引号?

[英]How to get rid of enclosing double-quotes in the expanded `forfiles` variables?

The forfiles command establishes several variables, indicated by a leading @ , which return data concerning the currently iterated item to the loop body. forfiles命令建立了几个变量,由前导@指示,这些变量将有关当前迭代项的数据返回到循环体。

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 .它们是: @file@fname@ext@path@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"

Assuming that C:\\root contains two files file1.txt and file2.txt , the output will be:假设C:\\root包含两个文件file1.txtfile2.txt ,输出将是:

".\file1.txt"
".\file2.txt"

However, I want the list of files without the surrounding "" .但是,我想要没有周围""的文件列表。

I am working on Windows 7 64-bit.我在 Windows 7 64 位上工作。

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嵌套for %I循环并使用%~I扩展 - 在命令提示符窗口中使用此代码:

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:要在批处理文件中使用该代码,您必须将% -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

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 ,因为forfiles删除(非转义)给定字符串中的双引号,例如/C之后的命令行:

forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\""

Or alternatively (the doubled inner forfiles is intentional, this works around a bug -- see this post ):或者(双倍的内部forfiles是有意的,这可以解决一个错误——请参阅这篇文章):

forfiles /P "C:\root" /M "*.txt" /C "forfiles forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\""

The inner forfiles will enumerate exactly one item, which is the one passed over by the outer loop.内部forfiles将精确枚举一项,即外部循环传递的一项。 Since @relpath is already expanded when the inner loop is executed, the quotes are removed as they are not escaped.由于@relpath在执行内部循环时已经展开,因此引号将被删除,因为它们没有被转义。

So the returned list of files looks like (again taking the sample files from the original question):所以返回的文件列表看起来像(再次从原始问题中获取示例文件):

.\file1.txt

.\file2.txt

The additional line-break between the lines is generated by forfiles .行之间的额外换行符由forfiles生成。 You can avoid that using redirection (dismiss forfiles output, but display only the echo output in the console window):您可以使用重定向来避免这种情况(关闭forfiles输出,但仅在控制台窗口中显示echo输出):

> nul forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C 0x22cmd /C > con echo @relpath0x22"

I remove the quotes like this:我删除这样的引号:

@ECHO OFF
GOTO START

 usage: 
 script.bat "*.txt" "c:\Documents"
 script.bat "*.txt"
 script.bat
 If no arguments added it will crawl the current directory with wildcard mask (*)
 Avoid root directory (c:\) because too many sub directories for the output console.

 :START
IF "%~2"=="" (SET "_FD=%CD%") ELSE (SET "_FD=%~2")
IF "%~1"=="" (SET "_MA=*") ELSE (SET "_MA=%~1")

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq delims=" %%A in (
        `forfiles /p %_FD% /s /m %_MA% /C "cmd /c ECHO @relpath"`
    ) DO (
    SET "myfile=%%~A"
    ECHO !myfile:~2!
)
ENDLOCAL   
GOTO :EOF

results:结果:

thumbnails\A0-1.jpg
thumbnails\new folder\img.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM