Monday 17 March 2014

Batch files: easy way of iterating over arguments

Here's the ease way of iterating over argument list passed to batch files (got it from stackoverlow topic). Used it to invoke ImageMagic's commands that involve their own elaborate interpretation of % operator.
rem im.bat
@echo off
setlocal enableDelayedExpansion
set argCount=0
for %%x in (%*) do set /A argCount+=1
set /a arg=0
for /l %%x in (1, 1, %argCount%) do (
 set /a arg=!arg!+1
 call im_impl.bat %%!arg!
)
endlocal
rem im_impl.bat
rem ImageMagic commands
convert %1_%%d.jpg[1-3] -resize 170x130 -set filename:f %%t_also.jpg %%[filename:f]
convert %1_%%d.jpg[1-4] -resize x109 -crop 124x109+3+0 +append %1_4small.jpg
Now it's possible to run im_impl.bat multiple times with different arguments just calling im.bat:
>im.bat clippy_small chandler_mojito