Windows batch file – how to loop through files in a directory

batch-filefor-loop

I have this batch:

for /f %%a IN ('dir /b *.pdf') do call convert.exe %%a

This gets every pdf file thats in the same folder as convert.exe. I want to be able to say where the PDF resides. What do I have to change?

Thanks!

Best Answer

If the directory name can be hardcoded, then it will be

for /f %%a IN ('dir /b /s "Disk:\Your\Directory\Name\*.pdf"') do call convert.exe %%a

Note that this will also return all .pdf files in subdirectories of Disk:\Your\Directory\Name.