Skip to content

Instantly share code, notes, and snippets.

@Luxiyalu
Created June 24, 2015 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luxiyalu/a96ccb637fe1e91faf10 to your computer and use it in GitHub Desktop.
Save Luxiyalu/a96ccb637fe1e91faf10 to your computer and use it in GitHub Desktop.
Rid the kindle "documents" directory of unnecessary *.sdr record files. Optional parameter: full path of the documents directory.
require 'FileUtils'
root = ARGV[0] || "/Volumes/Kindle/documents"
all_files = Dir[root + "/*"]
rec_files = all_files.find_all{ |f| f.end_with?(".sdr") }
# doc_types = ['txt', 'pdf', 'mobi', 'azw']
doc_files = all_files.find_all do |f|
f.end_with?("mobi") ||
f.end_with?("pdf") ||
f.end_with?("txt") ||
f.end_with?("azw")
end
doc_files_string = doc_files.join(" ").gsub(/\(|\)/, '')
rec_to_del = rec_files.reject do |f|
# have the same name
book_name = f.gsub(/\.sdr|\(|\)/, '').gsub(Regexp.new(root + "/"), '')
short_name = book_name.slice(0, 20)
book_exist = doc_files_string.match(short_name)
if !book_exist
print 'Deleted record: ', f, "\n"
end
book_exist
end
rec_to_del.each do |f|
FileUtils.rm_rf(f)
end
@Luxiyalu
Copy link
Author

Use it with:

$ ruby clean_up_kinde_documents_dir.rb /full/path/to/documents/folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment