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

坚强是说给别人听的谎言 提交于 2020-01-04 16:57:34

问题


The forfiles command establishes several variables, indicated by a leading @, which return data concerning the currently iterated item to the loop body.

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"

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 "".

I am working on Windows 7 64-bit.


回答1:


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

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\""

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\""

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

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"



回答2:


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


来源:https://stackoverflow.com/questions/32491883/how-to-get-rid-of-enclosing-double-quotes-in-the-expanded-forfiles-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!