Skip to content

Instantly share code, notes, and snippets.

@jeremywrnr
Created September 20, 2016 02:44
Show Gist options
  • Save jeremywrnr/daf57349fffca48081eca3d26f5bcf51 to your computer and use it in GitHub Desktop.
Save jeremywrnr/daf57349fffca48081eca3d26f5bcf51 to your computer and use it in GitHub Desktop.
renames a pdf file to author-year-title.pdf
#!/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")
@jeremywrnr
Copy link
Author

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