Skip to content

Instantly share code, notes, and snippets.

@sedrickcz
Last active February 16, 2018 23:21
Show Gist options
  • Save sedrickcz/ca172fa1d4272f19ab22688b29d00c4c to your computer and use it in GitHub Desktop.
Save sedrickcz/ca172fa1d4272f19ab22688b29d00c4c to your computer and use it in GitHub Desktop.
Create invoice list from directory
File.open("invoices.csv", 'w+') { |file|
file.write("user_id;waybill;invoice_file_name\n")
}
dir_path = File.join(Rails.root, 'private', 'invoices')
invoices = Dir.entries(dir_path)
invoices.each do |invoice|
next if ['.', '..', '.DS_Store'].include?(invoice)
# Split "026_4611873943_003915.pdf => [X, waybill, user_id]"
arr = invoice.split('_')
begin
# Split 003915.pdf and remove 00
id = arr[2].split('.').first.to_i
rescue
puts "ERORR => #{arr.inspect} => #{invoice}"
end
File.open("invoices.csv", 'a') { |file|
file.write("#{id};#{arr[1]};#{invoice}\n")
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment