Created
August 16, 2011 13:48
-
-
Save venj/1149120 to your computer and use it in GitHub Desktop.
Extract and Rename rar files downloaded from Wow Ebooks
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 | |
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