5.7 Example scripts 147
PhotoLapse (free), JPGVideo (free), or the free command-line tool
Ffmpeg. The last one is a cross-platform tool that requires the photos to
be numbered sequentially starting at 1 (e.g., frame_1.jpg, frame_2.
jpg, frame_3.jpg, ...). IrfanView allows you to do batch renaming of
photos. Alternatively, you could easily rename the images within the
camera before transferring them to the PC. The following Lua script is
all you need.
Renaming files
The following script can be used for renaming files. It uses “FRAME_” as a
filename prefix and starts numbering at 1. However, this can be easily
changed.
--[[
@title Rename files
@param s start value
@default s 1
--]]
root = "A/DCIM"
prefix = "FRAME_"
suffix = ".JPG"
pcall(function()
require("chdklib")
root = "TEST"
end
)
The header defines the script title and a parameter for the start value of
the sequence number. The directory root is set at A/DCIM, where the sub-
folders for the images are located. The following pcall() expression is
used for testing the script on a PC (section 5.8), where we also use a differ-
ent root directory.
The function renameFile() does the actual renaming with the help of
the operating system function os.rename(). But before we do so, we check
to see whether the file to be renamed is actually a file (and not a folder). We
also check to see whether a file with the new name already exists. In that
case, we throw an error message because giving a file the name of an al-
ready existing file can cause unexpected results. Function os.stat() is
used to do all that. It returns status information about the specified file
(section 5.4.13).