{"id":11819,"date":"2020-10-23T09:34:08","date_gmt":"2020-10-23T13:34:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=11819"},"modified":"2020-10-23T09:34:08","modified_gmt":"2020-10-23T13:34:08","slug":"avoid-overwriting-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2020\/10\/23\/avoid-overwriting-files\/","title":{"rendered":"Avoid Overwriting Files!"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/60179-avoid-file-overwrite\"><tt>Avoid file overwrite<\/tt><\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2630498\">Simon Musall<\/a>.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#af998f3b-d38a-400e-8333-7de61d7e44b5\">A cautionary tale<\/a><\/li><li><a href=\"#de4997fe-2759-4963-8f3b-99e28da03b66\">avoidOverwrite<\/a><\/li><\/ul><\/div><h4>A cautionary tale<a name=\"af998f3b-d38a-400e-8333-7de61d7e44b5\"><\/a><\/h4><p>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.<\/p><p>I plugged the thumb drive into a carrier, and in a for-loop, I started extracting the images, writing them to disk, <i>and deleting them from the thumb drive<\/i> 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!<\/p><p>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:<\/p><pre class=\"codeinput\">imds = imageDatastore(fullfile(matlabroot, <span class=\"string\">'toolbox'<\/span>, <span class=\"string\">'matlab'<\/span>, <span class=\"string\">'imagesci'<\/span>), <span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'FileExtensions'<\/span>, {<span class=\"string\">'.tif'<\/span>, <span class=\"string\">'.png'<\/span>});\r\nnumFiles = numel(imds.Files)\r\n<span class=\"comment\">% Create a write directory<\/span>\r\nwriteDir1 =  fullfile(userpath, <span class=\"string\">'Test1'<\/span>);\r\n<span class=\"keyword\">if<\/span> ~exist(writeDir1, <span class=\"string\">'dir'<\/span>)\r\n    mkdir(writeDir1)\r\n<span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">for<\/span> ii = 1:numFiles\r\n    img = readimage(imds, ii);\r\n    imwrite(img, fullfile(writeDir1, <span class=\"string\">'Oops.png'<\/span>));\r\n<span class=\"keyword\">end<\/span>\r\ndir(writeDir1)\r\n\r\n<span class=\"comment\">% Notice that while I _intended_ to write two files, I actually only end up<\/span>\r\n<span class=\"comment\">% with one--the last one! (The first image was overwritten!!!)<\/span>\r\n<\/pre><pre class=\"codeoutput\">numFiles =\r\n     2\r\n\r\n.         ..        Oops.png  \r\n\r\n<\/pre><h4>avoidOverwrite<a name=\"de4997fe-2759-4963-8f3b-99e28da03b66\"><\/a><\/h4><p>Now try again with Simon's <tt>avoidOverwrite<\/tt>: Create a second write directory<\/p><pre class=\"codeinput\">writeDir2 =  fullfile(userpath, <span class=\"string\">'Test2'<\/span>);\r\n<span class=\"keyword\">if<\/span> ~exist(writeDir2, <span class=\"string\">'dir'<\/span>)\r\n    mkdir(writeDir2)\r\n<span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">for<\/span> ii = 1:numFiles\r\n    img = readimage(imds, ii);\r\n    fn = avoidOverwrite(<span class=\"string\">'Oops.png'<\/span>, writeDir2, 2, 1)\r\n    imwrite(img, fullfile(writeDir2, fn))\r\n<span class=\"keyword\">end<\/span>\r\ndir(writeDir2)\r\n<\/pre><pre class=\"codeoutput\">fn =\r\n    'Oops.png'\r\nfn =\r\n    'Oops_01.png'\r\n\r\n.            ..           Oops.png     Oops_01.png  \r\n\r\n<\/pre><p>With the benefit of hindsight, <tt>avoidOverwrite<\/tt> 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!)<\/p><p>As always, I welcome your <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=11819#respond\">thoughts and comments<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_949840ac340a45beb60fd9dff7934368() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='949840ac340a45beb60fd9dff7934368 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 949840ac340a45beb60fd9dff7934368';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2020 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_949840ac340a45beb60fd9dff7934368()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2020b<br><\/p><\/div><!--\r\n949840ac340a45beb60fd9dff7934368 ##### SOURCE BEGIN #####\r\n%% Avoid Overwriting Files\r\n%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s\r\n% Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/60179-avoid-file-overwrite |Avoid file overwrite|>,\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2630498 Simon Musall>.\r\n%\r\n%% A cautionary tale\r\n% Some years ago, I did something so boneheaded that it still pains me to\r\n% think about today. After returning from a once-in-a-lifetime safari in\r\n% Namibia with a thumb drive full of thousands of photographs, and I set\r\n% about downloading the images to my hard drive with a snippet of code that\r\n% I whipped together in MATLAB.\r\n\r\n%%\r\n% I plugged the thumb drive into a carrier, and in a for-loop, I started\r\n% extracting the images, writing them to disk, _and deleting them from the thumb\r\n% drive_ as I went! Much to my dismay, I soon discovered that I had neglected to\r\n% change the filenames, and that each image was overwriting its\r\n% predecessor. D'Oh!\r\n\r\n%% \r\n% After much anguish, I was ultimately able to recover most of the images\r\n% from the thumb driveREPLACE_WITH_DASH_DASHbut I think I aged a decade on that day! Consider\r\n% how easy it is to make this horrific mistake:\r\n\r\nimds = imageDatastore(fullfile(matlabroot, 'toolbox', 'matlab', 'imagesci'), ...\r\n    'FileExtensions', {'.tif', '.png'});\r\nnumFiles = numel(imds.Files)\r\n% Create a write directory\r\nwriteDir1 =  fullfile(userpath, 'Test1');\r\nif ~exist(writeDir1, 'dir')\r\n    mkdir(writeDir1)\r\nend\r\nfor ii = 1:numFiles\r\n    img = readimage(imds, ii);\r\n    imwrite(img, fullfile(writeDir1, 'Oops.png'));\r\nend\r\ndir(writeDir1)\r\n\r\n% Notice that while I _intended_ to write two files, I actually only end up\r\n% with oneREPLACE_WITH_DASH_DASHthe last one! (The first image was overwritten!!!)\r\n\r\n%% avoidOverwrite\r\n% Now try again with Simon's |avoidOverwrite|:\r\n% Create a second write directory\r\nwriteDir2 =  fullfile(userpath, 'Test2');\r\nif ~exist(writeDir2, 'dir')\r\n    mkdir(writeDir2)\r\nend\r\nfor ii = 1:numFiles\r\n    img = readimage(imds, ii);\r\n    fn = avoidOverwrite('Oops.png', writeDir2, 2, 1)\r\n    imwrite(img, fullfile(writeDir2, fn)) \r\nend\r\ndir(writeDir2)\r\n\r\n%%\r\n% With the benefit of hindsight, |avoidOverwrite| would have saved me a\r\n% great deal of anguish! (Of course, if I had been thinking of this\r\n% potential failure mechanism, I would have avoided it from the start. But\r\n% that's a story for another day!)\r\n\r\n%%\r\n% As always, I welcome your\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=11819#respond thoughts and comments>.\r\n##### SOURCE END ##### 949840ac340a45beb60fd9dff7934368\r\n-->","protected":false},"excerpt":{"rendered":"<p>Brett's Pick this week is Avoid file overwrite, by Simon Musall.ContentsA cautionary taleavoidOverwriteA cautionary taleSome years ago, I did something so boneheaded that it still pains me to think... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2020\/10\/23\/avoid-overwriting-files\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11819"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=11819"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11819\/revisions"}],"predecessor-version":[{"id":11823,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11819\/revisions\/11823"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=11819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=11819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=11819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}