File Exchange Pick of the Week

Our best user submissions

Avoid Overwriting Files!

Brett's Pick this week is Avoid file overwrite, by Simon Musall.

Contents

A cautionary tale

Some years ago, I did something so boneheaded that it still pains me to think about today. After returning from a once-in-a-lifetime safari in Namibia with a thumb drive full of thousands of photographs, and I set about downloading the images to my hard drive with a snippet of code that I whipped together in MATLAB.

I plugged the thumb drive into a carrier, and in a for-loop, I started extracting the images, writing them to disk, and deleting them from the thumb drive as I went! Much to my dismay, I soon discovered that I had neglected to change the filenames, and that each image was overwriting its predecessor. D'Oh!

After much anguish, I was ultimately able to recover most of the images from the thumb drive--but I think I aged a decade on that day! Consider how easy it is to make this horrific mistake:

imds = imageDatastore(fullfile(matlabroot, 'toolbox', 'matlab', 'imagesci'), ...
    'FileExtensions', {'.tif', '.png'});
numFiles = numel(imds.Files)
% Create a write directory
writeDir1 =  fullfile(userpath, 'Test1');
if ~exist(writeDir1, 'dir')
    mkdir(writeDir1)
end
for ii = 1:numFiles
    img = readimage(imds, ii);
    imwrite(img, fullfile(writeDir1, 'Oops.png'));
end
dir(writeDir1)

% Notice that while I _intended_ to write two files, I actually only end up
% with one--the last one! (The first image was overwritten!!!)
numFiles =
     2

.         ..        Oops.png  

avoidOverwrite

Now try again with Simon's avoidOverwrite: Create a second write directory

writeDir2 =  fullfile(userpath, 'Test2');
if ~exist(writeDir2, 'dir')
    mkdir(writeDir2)
end
for ii = 1:numFiles
    img = readimage(imds, ii);
    fn = avoidOverwrite('Oops.png', writeDir2, 2, 1)
    imwrite(img, fullfile(writeDir2, fn))
end
dir(writeDir2)
fn =
    'Oops.png'
fn =
    'Oops_01.png'

.            ..           Oops.png     Oops_01.png  

With the benefit of hindsight, avoidOverwrite would have saved me a great deal of anguish! (Of course, if I had been thinking of this potential failure mechanism, I would have avoided it from the start. But that's a story for another day!)

As always, I welcome your thoughts and comments.




Published with MATLAB® R2020b

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.