Monday, May 08, 2006

[Tcl] Finding missing file(s)

Image you have 9000 files in a directory with a running sequence number like this:
photo.00001.jpg
photo.00002.jpg
...
photo.08999.jpg
photo.09000.jpg

I know I can count the number of files in the directory and realised there are 3 files missing
$ ls -1 photo.*.jpg | wc -l
8997

The question is: which 3 files

#! /usr/bin/tclsh


if { $argc != 3 } {
        puts stderr "Usage: $argv0 <start> <end> <pad>"
        exit 1
}
set start [lindex $argv 0]
set end [lindex $argv 1]
set pad [lindex $argv 2]


if { ! [string is integer $start] ||      ! [string is integer $end]   ||      ! [string is integer $pad] } {
        puts stderr "Error. <start> <end> <pad> have to be an integer"
        exit 2
}


set all [lsort [glob -nocomplain *]]
for { set i $start } { $i <= $end } { incr i } {
        set ipad [format "%0${pad}d" $i]
        set rc [lsearch -regexp $all "^.*\\.$ipad\\..*\$"]
        if { $rc < 0 } {
                puts $i
        }
}