Creating Sequentially Numbered File Names

QUESTION: I'm trying to create sequentially numbered image file names so I can put my image files into an animation I am creating. But I can't work out how to create the filenames in an IDL FOR loop. Can you help?

ANSWER: The only trick here is to format the loop index as a string. Of course, you can always create a string out of a number by using the String or (more often) the StrTrim functions:

   FOR j=0,15 DO Print, 'mymovie' + StrTrim(j,2) + '.tif'

The problem with this method is that the file names are different lengths. For example:


To make the file names all the same length, we need to use the "Ix.y" format code with the String function. So, for example, if we want our "number" to always be two digits, our code will look like this:

   FOR j=0,15 DO Print, 'mymovie' + String(j, Format='(I2.2)') + '.tif'

This results in file names that always have the same number of characters, with integers padded with zeros, as needed. For example:


This little tip so inspired Ben Tupper he "wrote a wrapper around this idea so I won't have to remember it ever again." He has offered it to us as the routine PadInteger.

Web Coyote's Guide to IDL Programming