简体   繁体   English

与FOR循环一起使用时,FORFILES不再递归

[英]FORFILES no longer recursive when used with FOR loop

I have this code below in a batch file (Windows). 我在批处理文件(Windows)中有以下代码。 It returns a list of paths for each .jpg file in a directory. 它返回目录中每个.jpg文件的路径列表。 Then passes that list to the ImageMagick Convert function (not the Windows convert) to create a pdf. 然后将该列表传递给ImageMagick转换函数(而不是Windows转换)以创建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. 这适用于返回jpg图像数据或其他单个文件类型。 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. 我需要它来搜索多个图像文件类型,我已经看到了这个示例解决方案,您可以将FORFILES放入FOR循环中。

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. forfiles/s不再有效。

I have tried FOR /R but it doesn't handle folder names with spaces which I need to be able to do. 我已经尝试过FOR /R但它不处理带有空格的文件夹名称,我需要能够做到。

Any thoughts on how to keep this recursive and not have issues with folder names with spaces? 有关如何保持这种递归的任何想法,并没有与空格的文件夹名称的问题?

@Lưu Vĩnh Phúc @LưuVĩnhPhú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. 我试过你说的但是For / F抛出一个错误,它认为.jpg是一个文件,它无法找到它。

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. 我删除/ r或/ f并将for循环中的变量添加到forfiles循环的位置。

This keeps its recursive nature and returns only the file types listed in the for loop. 这保持了它的递归性质,只返回for循环中列出的文件类型。

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

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