How to Recover files from a SD Card

Categories:   photography   tech  
Tags:   python   photography  

Have you ever accidentally deleted a bunch of images on your PC or have a SD Card fail on you?

If you act quickly, you might be able to recover these deleted images.

When you erase a file on your PC or SD Card, the actual files are not removed; the index that reference them is removed.

If you act quickly, you might be able to recover some, if not all of these files.

How to Recover deleted files

Turn off the device

The first thing to do when a device fails on you is stop using it. Turn the device off immediately.

This prevents your existing files from being overwritten.

Next I will introduce two software I personally used.

Easeus (Paid)

The Easeus software comes with an user friendly interface.

All you need to do is:

  • Insert the SD Card in your PC.
  • Load the Easeus software.
  • Select the SD Card or any drive you want to recover data from.
  • Let the software run.
  • The software will list out all your files and filetypes it managed to recover.
  • You can then select which files or filetypes you want to copy to a seperate location.

Pros

  • Very easy to use
  • Can sort files by filetype

Cons

  • Free version is very limited
  • Probably have to use paid version

You can get the Easeus recovery tool of your choice here: https://www.easeus.com/

PhotoRec (Free)

PhotoRec is a free software that also recovers lost files from your drive.

There are two potential issues with PhotoRec for non-tech savvy people though.

  1. It’s slightly technical
  2. The recovered files will be outputted across multiple folders, making it tedious to export.

Though it might look intimidating, once you run the software, it should prompt you on which drive you want to recover.

Since it’s pretty useless to have all your files scattered across multiple folders, I’ll share a python script to save all those files in one folder (guide down below.)

You can get Photorec here: https://www.cgsecurity.org/wiki/TestDisk_Download

In terms of performance, both software yields the same results but EaseUs is a bit faster and obviously easier.

How to recursively copy/paste files to a single folder with Python

I’ll share a quick python script to search and copy files recursively into one folder.

Prerequisites

  • Python installed on your PC
  • Basic use of Command line

Let’s create a folder called recover and inside it:

  • create folder called input
  • create folder called output
  • create file called copy_files_over.py

Drag and drop all the folders you want searched in the input folder.

This is what the copy_files_over.py looks like:

import os
import shutil

source = './input' #select directory to traverse through
destination = './output' #select the output directory

for dirpath, dirnames, filenames in os.walk(source): #traverse through the source directory
    for f in filenames:
        files = (os.path.join(dirpath, f)) #the full path of the source files
        newpath = (os.path.join(destination, f)) #the destination of save file

        print(f'copying {files} to {newpath}') #print a generic message
        shutil.copy2(files, newpath) #copy/paste the filtered files to save location

Run the python script:

python copy_files_over.py

All files that were scattered inside the input folder will now be copied over to the output folder.

The above script is handy for recovering images from a SD Card.

If you want to filter specific filetypes and filesizes, you can use the script below instead:

import os
import shutil

#1 A list of extensions
exts = [
    '.txt',     #0
    '.gif',     #1
    '.jpg',     #2
    '.png',     #3
    '.dng',     #4
    '.nef',     #5
    '.avi',     #6
    '.m4v',     #7
    '.mkv',     #8
    '.mov',     #9
    '.mp4',     #10
    '.mpg',     #11
    '.wmv'      #12
    ]

#2 A list of file sizes
sizes = [
    10000000,     #0 - 10MB
    100000000,    #1 - 100MB
    1000000000,   #2 - 1GB
]

#3 set the variables here
ftype = exts[0] #pick extension no. from above list
fsize = sizes[0] #pick filesize no. from above list
source = './input' #select directory to traverse through
destination = './output' #select the output directory

for dirpath, dirnames, filenames in os.walk(source): #traverse through the source directory
    for f in filenames:
        ext = os.path.splitext(f)[-1] #get the files extension
        files = (os.path.join(dirpath, f)) #the full path of the source files
        newpath = (os.path.join(destination, f)) #the destination of save file
        size = os.stat(files).st_size #filesize info in bytes
        sizeinmb = round(size/1000000, 2) #filesize rounded off to 2 decimal places

        if ext == ftype: #4a use this to filter by filetype only
        if ext == ftype and size < fsize: #4b use this to filter by filetype & filesize
            print(f'copying {files} to {newpath} filesize: {sizeinmb}MB') #print a generic message
            shutil.copy2(files, newpath) #copy/paste the filtered files to save location

Just choose the extension and filesize you want to filter and it will output all those files to the output folder.

This script is useful for recovering specific data from huge hard discs with lots other data.

Related Products



Categories:   photography   tech  
Tags:   python   photography