{"id":39,"date":"2019-10-02T22:54:08","date_gmt":"2019-10-02T22:54:08","guid":{"rendered":"http:\/\/iheinrich.com\/?p=39"},"modified":"2019-10-02T22:54:08","modified_gmt":"2019-10-02T22:54:08","slug":"ive-been-duped","status":"publish","type":"post","link":"https:\/\/iheinrich.com\/index.php\/2019\/10\/02\/ive-been-duped\/","title":{"rendered":"I&#8217;ve been duped!"},"content":{"rendered":"\n<p>In testing my Photo Organizing Script, I noticed something scary.<\/p>\n\n\n\n<p>If I move an image into a folder where another DIFFERENT image has the SAME name, the move function overwrites previous image.<\/p>\n\n\n\n<p>No warning.<\/p>\n\n\n\n<p>This is a pretty serious issue, since it will result in <strong>data loss<\/strong>.<\/p>\n\n\n\n<p>It\u2019s time to write a function to detect for this and rename the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tdef move_safely (file, folderName)\n\n\t  if File.exists?(folderName+\"\/\"+file)\n\t    puts \"the file exists and should not be overwritten!\"\n\n\t    #rename the file\n\t    #generate an 8 char random string to append to the file name.\n\t    random8 = (0...8).map{(65+rand(26)).chr}.join\n\n\t    #break up the file into file and file type. The result of this is an array\n\t    breakUp = file.split(\".\")\n\n\t    #add the fixin's to the random8 string\n\t    randomize = \"_\"+random8+\".\"\n\n\t    #insert it into the array\n\t    breakUp.insert(1,randomize)\n\n\t    #convert the array into a file string\n\t    newFileName = breakUp.join\n\n\t    File.rename(file,newFileName)\n\n\t    file = newFileName\n\n\t  else\n\t    puts \"the file doesn't exist\"\n\t  end\n\n\tFileUtils.move file, folderName\n\n\tend\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In testing my Photo Organizing Script, I noticed something scary. If I move an image into a folder where another&#8230;<\/p>\n","protected":false},"author":1,"featured_media":40,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby","wpcat-5-id"],"_links":{"self":[{"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":1,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":41,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions\/41"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/media\/40"}],"wp:attachment":[{"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iheinrich.com\/index.php\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}