简体   繁体   中英

FORFILES no longer recursive when used with FOR loop

I have this code below in a batch file (Windows). It returns a list of paths for each .jpg file in a directory. Then passes that list to the ImageMagick Convert function (not the Windows convert) to create a pdf.

echo moving CMD to drive location at %~dp1
CD /d %~dp1

echo getting the list of file names
FORFILES /p %~dp1 /s /m "*.jpg" /C "cmd /c echo @path" > files.txt

echo creating the pdf using ImageMagick
convert @files.txt test.pdf

This works fine for returning the jpg image data or another single file type. I need it to search for multiple image file types and I have seen this example solution where you can put the FORFILES into a FOR loop.

for %%G in ( .jpg , .tif ) do FORFILES /p %~dp1 /s /m "*.jpg" /C "cmd /c echo @path" > files.txt

But if I do that the program is no longer recursive. The /s in forfiles no longer works.

I have tried FOR /R but it doesn't handle folder names with spaces which I need to be able to do.

Any thoughts on how to keep this recursive and not have issues with folder names with spaces?

@Lưu Vĩnh Phúc

thank you for responding. I tried what you said but For /F was throwing an error where it thought .jpg was a file and it couldnt find it.

You did force me to try new things and in doing so I think it was just my syntax that was the issue. I changed the code to this:

FOR %%G in (.jpg, .tif) do FORFILES /p %~dp1 /s /m *%%G /C "cmd /c echo @path" >> files.txt

Where I removed the /r or /f and added the variable from the for loop to the forfiles loop.

This keeps its recursive nature and returns only the file types listed in the for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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