1

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?

Community
  • 1
  • 1
joacampb
  • 177
  • 1
  • 18
  • `for /r` lists the whole file name, so just wrap the variable with quotes or use `for /F "usebackq tokens=*" %a in ('%a')` – phuclv Apr 11 '16 at 03:34

1 Answers1

0

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

joacampb
  • 177
  • 1
  • 18
  • 1
    Actually, that does not work for me. FORFILES /M option does not work if the path ends with \, and `%~dp1` always ends with \. It only works if I use `/m %~dp1.`. – dbenham Apr 11 '16 at 04:54