# Attempt to save (recover) files from a broken external drive

My external drive that I use for Backup had some broken files. I feared that I'd lose some (or all) the data in there. So I wanted to copy each and every file to my hard disk.

I first tried using regular Windows methods (Copy, Xcopy). But, these methods took a lot of time and the commands would stop execution after hitting a broken file.

For example:

xcopy E:\sampleSourceDirectory\*.* D:\BACKUP\sampleSourceDirectory\ /s /e


Solution Method:

1. An iterative script to try to move each file in the sampleSourceDirectory
2. Report how many files have failed

Note: I preferred MOVING files, instead of COPYING, so that I could retry moving the folder again, hoping that the external drive can read the files upon another trial.

Python script:

import shutil
import os

folderName = 'sampleSourceDirectory'

source = 'E:/' + folderName + '/'
destination = 'D:/BACKUP/' + folderName + '/'

if (os.path.exists(destination) == False):
os.makedirs(destination)    #Make that Directory

files = os.listdir(source)
filesSource = files

nSuccess = 0        # number of Files
nFail = 0

while len(files)>0:
for f in files:
try:
shutil.move(source + f, destination + f)
nSuccess = nSuccess + 1
except OSError as err:
print("OS error: {0}".format(err))
files.remove(f)
nFail = nFail + 1

filesDestination = os.listdir(destination)
if (filesSource == filesDestination) and (len(filesSource) != 0):
print("Move Successful")
else:
print("ERROR IN MOVE!!! ****** CHECK WARNINGS!!! \n")
print ( nFail, ' files failed!')


With this question, I'd like to first answer the basic question: is this an effective way to try saving files?

Secondly, I'd like to receive general feedback on the script itself, and any particular points that I might have misthought/misimplemented.

Final note: this script is not intended to be a secure way and I'd strongly advise not to use it for serious data. (I don't know if it's by chance or not but after a few trials my external drive stopped working completely, and I could only save 10% of the contents.)

• My recommendation, if your filesystem might be broken: make a byte-for-byte clone of the filesystem before you do making any modifications to it that might worsen the situation. – 200_success Sep 19 '18 at 18:58
• @200_success thanks for the info and the link. I think I should take the safer route (as you mentioned). however I wanted to experiment with this idea and I had accepted losing some of the files on the way. It turned out to be a big loss (90% lost). I also tried using checkdisk to correct the file system; but it didn’t work. Btw, we had Norton disk doctor in the 90’s, to deal with such issues. Moving files may have really resulted in this mess. :( – Gürkan Çetin Sep 19 '18 at 19:09
• You should use ROBOCOPY not XCopy. All the issues you mention have switches to mitigate errors, verify CRC, only copy differences, etc. – C. Harley Sep 27 '18 at 3:20
• @C.Harley thank you, the drive is not responding as of now. :( so I cannot try. – Gürkan Çetin Sep 27 '18 at 18:32