{"id":296,"date":"2015-05-14T16:29:56","date_gmt":"2015-05-14T16:29:56","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=296"},"modified":"2015-05-14T16:29:56","modified_gmt":"2015-05-14T16:29:56","slug":"ruby-parse-directory-rename-files-copy-contents-to-another-folder-for-additional-processing","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=296","title":{"rendered":"Ruby &#8211; Parse Directory &#8211; Rename Files &#8211; Copy contents to another folder for additional processing"},"content":{"rendered":"<pre>\n# split, pop and strip file_path to return file_name\ndef prepFileName(file_path, ext, ds = \"\/\")\n    path_array = file_path.split(ds)\n    file_name = path_array.pop\n    return file_name.sub(ext, '')\nend\n\ndef createNewFilePath(file_path, new_location, ext)\n    unless new_location.nil?\n        file_path = new_location + prepFileName(file_path, ext)\n    end\n    return file_path\nend\n\ndef writeFile(file_path, contents, new_location, ext)\n    file_path = createNewFilePath(file_path, new_location, ext)\n    File.open(file_path, 'w') { |file| file.write(contents) }\nend\n\ndef readFile(file_path)\n    return File.read(file_path)\nend\n\ndef readDir(dir)\n    return Dir[ File.join(dir, '**', '*') ].reject { |p| File.directory? p }\nend\n\ndir = nil\narray_of_all_files = nil\n\n# Should be a hash?\n\narray_of_all_files = readDir(ARGV[0])\n\narray_of_all_files.each do |file_path|\n    contents = readFile(file_path)\n    writeFile(file_path, contents, ARGV[1], ARGV[2])\nend\n\nputs array_of_all_files\n<\/pre>\n<h1>use<\/h1>\n<p>ruby dir.rb .\/templates\/ .\/ .hbs.hamlbars<\/p>\n","protected":false},"excerpt":{"rendered":"<p># split, pop and strip file_path to return file_name def prepFileName(file_path, ext, ds = &#8220;\/&#8221;) path_array = file_path.split(ds) file_name = path_array.pop return file_name.sub(ext, &#8221;) end def createNewFilePath(file_path, new_location, ext) unless new_location.nil? file_path = new_location + prepFileName(file_path, ext) end return file_path end def writeFile(file_path, contents, new_location, ext) file_path = createNewFilePath(file_path, new_location, ext) File.open(file_path, &#8216;w&#8217;) { |file| [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=296"}],"version-history":[{"count":1,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":297,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/296\/revisions\/297"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}