DOS For loop for iterating

Sunday, 13 December, 2015

I’m currently working on a pet project doing some timelapse (if you hadn’t guessed!) involved geocoding the photos (and a much longer blog on this process at a later date once it’s complete). However as part of this I needed to iterate over a series of files using the excellent nconvert. DOS batch files let you do this but the syntax (for me at least!) is a little confusing. So, in all it’s glory here’s the solution I rustled up after looking at various websites:

for /f %%a IN (‘dir /b .jpg’) do ..\nconvert -out jpeg -wmflag bottom-right -wmfile overlay\%%~na.png -o final\%%~na.jpg %%~na.jpg

As ever, much useful information can be gleaned by typing

for /?

The /f parses a list of files (denotes by the variable %%a) and this is fed to it by the ‘dir /b *.jpg’ command. In this case a “bare” directory listing of all JPG files. Once that is complete it runs the nconvert command inserting the variable at the %%a locations.

Crucially the statement is then finished with \%%~na.jpg which strips the file
name* from the 3-letter filetype (suffix). This is needed in the nconvert command.

One line of code, 1000 files processed very rapidly! Love it when this stuff works!!

Add comment

Fill out the form below to add your own comments