All times are UTC - 5 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Contribution - Script to sort testdisk/photorec results
PostPosted: May 12th, 2012, 17:22 
Offline

Joined: April 5th, 2012, 3:48
Posts: 13
Location: Spain
Hi all,

Those of you using testdisk/photorec free software for recovering lost files shall know what means searching specific file types as images in results.

I just did a short python script that sorts all the files extracted by testdisk/photorec in folders sorted by filetype (images, pdf, video, etc).

You can check it too in my website at http://system-tricks.com/index.php/data ... y-results/

Hope you find it useful.

Code:
#!/usr/bin/env python
import os, magic
from shutil import copyfile
#================ CONFIG SECTION ================#
# Source folder with photorec/testdisk results:
testdisk_results = 'SOURCE FOLDER WHERE TESTDISK RESULTS ARE'
#
# Destination folder where you want to store the sorted results:
base_destdir = 'DESTINATION FOLDER WHERE YOU WANT TO STORE SORTED RESULTS'
#
# File types to filter out. You can add every MIME-types you are not interested on:
filter = ['application_octet-stream', 'text_plain']
#============= END OF CONFIG SECTION ============#
types = {}
mime = magic.open(magic.MAGIC_MIME)
mime.load()
for dir in os.listdir(testdisk_results):
    for filename in os.listdir(os.path.join(testdisk_results, dir)):
        file = os.path.join(testdisk_results, dir, filename)
        filetype = mime.file(file).split(';')[0].replace('/', '_').replace(" ", "_").split(',')[0]
        if filetype in filter:
            continue
        if not filetype in types:
            types[filetype] = 1
        else:
            types[filetype] += 1
        print filename + ": " + filetype
        destdir = os.path.join(base_destdir, filetype)
        destfile = os.path.join(destdir, filename)
        if os.path.exists(destdir):
            try:
                copyfile(file, destfile)
            except Exception, e:
                print "Error copying " + filename + ": " + str(e)
                continue
        else:
            os.makedirs(destdir)
            try:
                copyfile(file, destfile)
            except Exception, e:
                print "Error copying " + filename + ": " + str(e)
                continue
print "\n** Results:\n--------------------"
for type in types:
    print type + ": " + str(types[type]) + " files"
print print "\n---------------------------\n\n* Finished. Results stored in " + base_destdir + "\n\n"


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group