Ruby – Parse Directory – Rename Files – Copy contents to another folder for additional processing

# split, pop and strip file_path to return file_name
def prepFileName(file_path, ext, ds = "/")
    path_array = file_path.split(ds)
    file_name = path_array.pop
    return file_name.sub(ext, '')
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, 'w') { |file| file.write(contents) }
end

def readFile(file_path)
    return File.read(file_path)
end

def readDir(dir)
    return Dir[ File.join(dir, '**', '*') ].reject { |p| File.directory? p }
end

dir = nil
array_of_all_files = nil

# Should be a hash?

array_of_all_files = readDir(ARGV[0])

array_of_all_files.each do |file_path|
    contents = readFile(file_path)
    writeFile(file_path, contents, ARGV[1], ARGV[2])
end

puts array_of_all_files

use

ruby dir.rb ./templates/ ./ .hbs.hamlbars

Leave a Reply

Your email address will not be published. Required fields are marked *