Tuesday, 24 January 2012

Windows batch scripting

I spent most of today trying to write a windows batch file to work with ImageMagick to create the process the images for the slit scan style turntable photo.

I found a reasonably good tutorial on Windows batch script writing here: Win32 Shell Scripting Tutorial. However, it didn't cover everything I needed. One of the things was that a command line variable was always quoted, so when I used it in a string, the string looked like this for example:

echo Hello %1 Bye
Hello "This is the contents of a variable that was set on the command line" Bye

I found a solution here: Remove quotes from named environement variables in Windows scripts, but then found that actually when passing the variable to a command, it didn't mind that the variable was enclosed in quotes anyway, e.g.

convert "%%i" -quiet -crop 0x%i%+0+!offset! %1/files/%%~nxi

Worked fine

Next I found that my %offset% variable wasn't updating in a loop. Eventually I found the answer to that here: Why That Batch For Loop Isn’t Working. It turns out that if you want to get the current value of a variable updated in a loop, you need to specify setlocal EnableDelayedExpansion before the loop, and then access the variable using ! exclamation marks instead of % percent signs, e.g. !offset! instead of %offset%.

Another thing was how to store the output of a command in a variable. In bash, you do this using backticks. For windows batch scripting, I found the answer here: Batch equivalent of Bash backticks. So for getting the height of an image into a variable, I had to do this:

FOR /F %%x IN ('identify -quiet -format "%%h" "%%i"') DO SET /A height=%%x

There were numerous other problems I had, but I can't specify them all here.

No comments: