2007-12-30

Managing JPG and CR2 files

My latest toy is a Canon Digital Rebel XTi, which has the option of shooting in JPG and RAW, or both. I don't have a lot of use for RAW (CR2 on the XTi) images right now, but I might someday. And with space as cheap as it is, I set the camera to shoot in both. They both end up in the same directory, but only the JPGs show up in GQView on Xubuntu. So, I go through and delete the JPG version of what I don't want -- CR2s don't show up in the file browser. I mean, it would be easy to go through by hand and delete CR2s if an associated JPG does not exist. But here in the land of over-engineering, we create a script to do it!

I think this would be a lot easier in bash, but I don't know bash scripting. So... quick and dirty? Yea, PHP is my language of choice.

#!/usr/bin/php
function sysout($message) {
fwrite(STDOUT, $message);
}
$cr2Extension = ".CR2";
$jpgExtension = ".JPG";

if ($argc < 1) {
sysout("No directory specified, exiting.\n");
exit(-1);
}
$directory = $argv[1];
if (!is_dir($directory)) {
sysout("`$directory` is not a directory, exiting.\n");
exit(-1);
}

sysout("Working directory: $directory\n");
sysout("Removing CR2 files if JPG does not exist...\n");
$files = scandir($directory);

$count = 0;
foreach ($files as $file) {
if (strripos($file, $cr2Extension) > -1) {
sysout("Found CR2: $file\n");
$jpg = $directory . substr($file, 0, strripos($file, ".")) . $jpgExtension;
if (!is_file($jpg)) {
sysout("Deleting: $directory$file\n");
unlink($directory . $file);
$count++;
}
}
}
sysout("Process ended successfully. $count files deleted.\n");
exit(0);
?>
If anyone wants to enlighten me to the 5 line bash or Python solution, by all means. This is easily adapted to Nikon style cameras by changing $cr2Extension to something else.

1 comment:

Chuck said...

I'm surprised the Cr2's don't show up in the hidden files... Ubuntu is on the bash shell, right?