best way to delete oldest images? - Script Writing - CHDK Forum supplierdeeply

best way to delete oldest images?

  • 2 Replies
best way to delete oldest images?
« on: 31 / August / 2011, 11:40:28 »
I want to add functionality to a timelapse script to delete the oldest file if there are more than n files, where n will be ~1000 files. This will give me a week or so of backup in case files aren't uploaded properly (camera may be inaccessible at times). But I can't rely on each image to be sequentially numbered because there will be more than 9999 images, so I want to rename the images to mmddhhmm.jpg after they are taken.

I am struggling with how to most efficiently do this.

it seems like I will want to scan for multiple ###canon subdirs in case the camera makes new ones, but I don't know if it makes sense to scan the whole camera each time, or to log the most recent dir to a file and read it, then scan if I don't find imgxxxx+1. Likewise I should be able to predict what the next filename will be if I just renamed one, and only scan for a file if not exist imgxxxx+1

the camera gets power cycled every few hours, so I need to keep track of image names in a file or do it with a table, but I am not sure what will be the most robust/efficient (power cycle could interrupt script at any point). Image intervals are ~10 minutes, so I have some time for execution, but shooting for efficiency too.

does anyone have pointers for where to start? Thanks for any help you can give.
(cameras are SX30 and SX220)
A720 1.00c | D10 1.00b |SX220 1.01a | SX230 ? | SX30 1.00p CHDK ver. 1.1.0-r1727

Re: best way to delete oldest images?
« Reply #1 on: 01 / September / 2011, 22:55:09 »
OK, I think I have skeleton code to rename IMG_####.jpg files to mmddHHMM.jpg.

So far it's not elegant - every time it's run it will loop through every directory under DCIM. But I can clean that up later... I am using lua libs 0.7 from mattkime's compiliation of scripts by fbonomi and others.

I would love any feedback from expert script writers. Especially about syntax errors, optimizing, and if I might have problems running this on a memory card with ~1000 images. Here's my code:
Code: [Select]
newPicList = disk.filterImageFiles("IMG_")
for index,oldName in ipairs(newPicList) do
fileInfo = os.stat(oldName)
newName ="%m%d%H%M",fileInfo.mtime)
picDir = string.sub(oldName,1,16)
and here is the code for the disk.filterImageFiles function and the disk.getImageList function it depends on:
Code: [Select]
-- returns list of files that contain the provided search string
function disk.filterImageFiles(searchString,directory)
--log.print("disk.filterImageFiles - start")
local imageList, strStart
if(directory == nil) then
imageList = disk.getImageList()
strStart = 17
imageList = os.listdir("A/DCIM/",false)
strStart = 1

local count = table.getn(imageList)
--log.print("disk.filterImageFiles - count:",count)
local i = 0
while i ~= count do
i = i + 1
if(string.find(imageList[i],searchString,strStart, true) == nil) then
i = i - 1
count = count - 1
--log.print("disk.filterImageFiles - end ")
return imageList

--[[ Loops through DCIM dirs and creates list of files ]]
--optionally take directory name?
function disk.getImageList()
local imgTable = {}
local dcimList = disk.getImageDirectories()
local dirCount = table.getn(dcimList)
local i = 0
while ( dirCount ~= i) do
i = i + 1
local imgDirList = os.listdir(dcimList[i], false)
local imgCount = table.getn(imgDirList)
local a = 0
while (imgCount ~= a) do
a = a + 1
return imgTable
A720 1.00c | D10 1.00b |SX220 1.01a | SX230 ? | SX30 1.00p CHDK ver. 1.1.0-r1727


Offline reyalp

  • ******
  • 14096
Re: best way to delete oldest images?
« Reply #2 on: 03 / September / 2011, 18:57:11 »
I'm not sure it's worth renaming them. If you are going to loop through all the images and look the timestamp with os.stat, you could just delete everything older than a certain time right then. The timestamp returned by os.time should be compatible with the timestamps in stat.

You do have to be careful listing lots of files, you can run out of memory. Because of this, it would be better to process the DCIM directories individually rather than building a master list of all the files. But if you don't run into trouble with the number of images you expect to have, you can ignore that. How likely this is to be a problem depends on how much free memory your camera starts with. has some background.

Lua has automatic garbage collection, but it doesn't know anything about how much free memory the camera has, so in some circumstances you can run out of free memory even though there is lots of uncollected garbage.  You may find the garbage collection functions helpful in this case:

Don't forget what the H stands for.


Related Topics

SimplePortal 2.3.6 © 2008-2014, SimplePortal