Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch file - how to loop through files in a directory?

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!

like image 528
EOB Avatar asked Jan 12 '12 14:01

EOB


People also ask

How do I loop through a folder?

To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.

How do you sequentially execute commands in batch file?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.


1 Answers

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.

like image 134
Sergey Kudriavtsev Avatar answered Oct 03 '22 08:10

Sergey Kudriavtsev