bashpdftoppm

Bad file names in bash loop


Intent

I am trying to loop through a directory and convert all the 9000 or so pdf files to png

for i in *.pdf; do
    pdftoppm -f 1 -l 2 -png $i ${i%.pdf*}
done

The Problem

the loop only works for about 500 cases. When I manually call pdftoppm on some of the pdf files that did not convert, it works.

For example, one file that doesn't work in the loop is w7hnp - FOO BAR 8-18-12 NOT FINAL DOC.pdf. When I use tab-completion to manually call pdftoppm the filename includes escape characters: w7hnp\ -\ FOO\ BAR\ 8-18-12\ NOT\ FINAL\ DOC.pdf and the command works fine.

Is there a way to ensure the spaces are properly escaped?

I need to preserve the file names to match against other info in a database, so renaming won't work unless I can create a dictionary with old and new names.


Solution

  • You should quote the variables like this: "$i" and "${i%.pdf*}"