Created
June 24, 2015 02:47
-
-
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.
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it with: