Skip to content

Instantly share code, notes, and snippets.

@venj
Created August 16, 2011 13:48
Show Gist options
  • Save venj/1149120 to your computer and use it in GitHub Desktop.
Save venj/1149120 to your computer and use it in GitHub Desktop.
Extract and Rename rar files downloaded from Wow Ebooks
#!/usr/bin/env ruby
require "fileutils"
include FileUtils
#
# Now you can fire up the command like this:
# ./rename.rb New.Riders.Flexible.Web.Design.Dec.2008.rar Flexible Web Design
#
# if there's any special characters in the REAL file name,
# escape them to prevent the shell to parse it.
#
(puts "Usage: #{File.basename($0)} rar_name book_name";exit 0) if ARGV.size < 2
(puts "\"unrar\" is not found on your system."; exit 1) unless system("which unrar > /dev/null 2>&1")
pack_name = ARGV[0];book_name = ARGV[1..-1].join(" ")
(puts "\"#{pack_name}\" does not exist."; exit 1) unless File.exist?(pack_name)
current_dir = pwd
mkdir(book_name)
system %|unrar x #{pack_name} "#{book_name}"|
cd book_name
rm "Readme.png"
files = Dir["**/*"].to_a
if files.size == 1
f = files.first
extension = f.split('.').last
new_name = %|#{book_name}.#{extension}|
mv f, new_name
mv new_name, current_dir
cd current_dir
rmdir book_name
else
files.each do |f|
extension = f.split('.').last
mv f, %|#{book_name}.#{extension}|
end
cd current_dir
end
puts "\nExtract and Rename Finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment