Last active
February 16, 2018 23:21
-
-
Save sedrickcz/ca172fa1d4272f19ab22688b29d00c4c to your computer and use it in GitHub Desktop.
Create invoice list from directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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