Enhanced "After Using PhotoRec" script

Using PhotoRec to recover lost data
Forum rules
When asking for technical support:
- Search for posts on the same topic before posting a new question.
- Give clear, specific information in the title of your post.
- Include as many details as you can, MOST POSTS WILL GET ONLY ONE OR TWO ANSWERS.
- Post a follow up with a "Thank you" or "This worked!"
- When you learn something, use that knowledge to HELP ANOTHER USER LATER.
Before posting, please read https://www.cgsecurity.org/testdisk.pdf
Post Reply
Message
Author
UlfZibis
Posts: 3
Joined: 03 Jan 2018, 00:11

Enhanced "After Using PhotoRec" script

#1 Post by UlfZibis » 03 Jan 2018, 00:35

Hi all,

This is my first post in this forum, so please be tolerant if I do formal mistakes.

In the wiki on https://www.cgsecurity.org/wiki/After_Using_PhotoRec there is a script for recovering the original file names from a Canon camera. It does not work for my files, recovered from a Canon A2200 SD-card on my Ubuntu machine, because the respective tag values do not begin solely with "100". Also the path for jhead matched bad and a leading Shebang may be helpful.

I have enhanced the script which now works fine:

Code: Select all

#!/usr/bin/perl -w
$working_dir = '.';
$jhead_bin = '/usr/bin/jhead';

@recovered_files = `ls $working_dir`;
foreach $file (@recovered_files) {
    chomp $file;
    @exif = `$jhead_bin -v $working_dir/$file`;
    foreach $line (@exif) { 
        if ($line =~ /Canon maker tag 0008 Value = [1-9]\d\d(\d{1,8})$/) {
            system("mv $working_dir/$file $working_dir/IMG_$1.JPG");
            print "IMG_$1.JPG from $file\n";
            last;
        }
    }
}
Also a hint may be useful, that jhead must first be installed, e.g. on Ubuntu with:

Code: Select all

sudo apt install jhead
Kind regards
Ulf
Last edited by UlfZibis on 16 Jan 2018, 11:59, edited 1 time in total.

Sponsored links

UlfZibis
Posts: 3
Joined: 03 Jan 2018, 00:11

Re: Enhanced "After Using PhotoRec" script

#2 Post by UlfZibis » 04 Jan 2018, 18:05

An additional enhancement allows to pass the directory to work on by a command line argument:

Code: Select all

#!/usr/bin/perl -w
$jhead_bin = '/usr/bin/jhead';
$dir = (@ARGV > 0) ? $ARGV[0] : '.';
$dir =~ s/\/*$//;    # truncate trailing '/'s

@recovered_files = `ls $dir`;
foreach $file (@recovered_files) {
    chomp $file;
    @exif = `$jhead_bin -v $dir/$file`;
    foreach $line (@exif) { 
        if ($line =~ /Canon maker tag 0008 Value = [1-9]\d\d(\d{1,8})$/) {
            system("mv $dir/$file $dir/IMG_$1.JPG");
            print "IMG_$1.JPG from $file\n";
            last;
        }
    }
}
Last edited by UlfZibis on 16 Jan 2018, 12:01, edited 2 times in total.

UlfZibis
Posts: 3
Joined: 03 Jan 2018, 00:11

Re: Enhanced "After Using PhotoRec" script

#3 Post by UlfZibis » 04 Jan 2018, 18:10

The following script allows to additionally recreate the original foldertree on the a Canon camera (tested with SD-card from Canon PowerShot A2200):

Code: Select all

#!/usr/bin/perl -w
$jhead_bin = '/usr/bin/jhead';
$dir = (@ARGV > 0) ? $ARGV[0] : '.';
$dir =~ s/\/*$//;    # truncate trailing '/'s

@recovered_files = `ls $dir`;
foreach $file (@recovered_files) {
    chomp $file;
    @exif = `$jhead_bin -v $dir/$file`;
    $folder_no = '000';
    $picture_no = '0000';
    $date = '0000';
    foreach $line (@exif) {
        if ($line =~ /Canon maker tag 0008 Value = ([1-9]\d\d)(\d{1,8})$/) {
            $folder_no = $1;
            $picture_no = $2;
        }
        if ($line =~ /Time\s*:\s*\d{4}:(\d\d):(\d\d)\s[\d:]{8}$/) {
            $date = "$2$1";
            last;
        }
    }
    unless ($date == '0000') {
        print "$folder_no\_$date/IMG_$picture_no.JPG from $file\n";
        system("mkdir -p $dir/$folder_no\_$date");
        system("mv $dir/$file $dir/$folder_no\_$date/IMG_$picture_no.JPG");
    }
}

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest