Created
September 20, 2016 02:44
-
-
Save jeremywrnr/daf57349fffca48081eca3d26f5bcf51 to your computer and use it in GitHub Desktop.
renames a pdf file to author-year-title.pdf
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
#!/usr/bin/env ruby | |
# scholar-rename | |
# prereq: pdftotext installation | |
# renames a pdf file to author-year-title.pdf | |
# confirms with user before moving | |
# warning: accurate ~ 50% of time | |
system("pdftotext -q #{ARGV.first} temptext") | |
author = '' | |
title = '' | |
yr = nil | |
File.open("temptext").each_with_index do |line, n| | |
if n == 0 | |
title = line.chomp.gsub(' ', '_') | |
elsif n == 1 | |
author = line.sub(',.*', '').split.last | |
elsif yr.nil? | |
test = /((19|20)\d\d)/.match(line) | |
yr = test.to_s if test | |
end | |
end | |
# display name | |
yr = '' if yr.nil? | |
name = author.upcase + '_' + yr + '_' + title + '.pdf' | |
# cleanup | |
puts name | |
printf 'Ok? [yN]: ' | |
conf = STDIN.gets.chomp | |
File.rename(ARGV.first, name) if conf.match /(y|Y).*/ | |
File.delete("temptext") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated: https://github.com/jeremywrnr/scholar-rename