An update to my dumb little script - now renames short pdf or epub
names to the folder name - strips out periods, and word-capitalises the
filename.
# de-psycho USENET posts
require 'rubygems'
require 'zip'
require 'fileutils'
class Desyco
def process_folders(dirfilter,destination, moveto='done')
Dir.glob(dirfilter).each do |dir|
next unless File.directory? dir
next if dir == moveto
puts "Processing #{dir}"
Dir.chdir dir
files = Dir.glob("*.zip")
files.each do |file|
extract file
end
Dir.chdir("..")
# now unrar whatever came out
files = Dir.glob("#{escape_glob(dir)}/*.rar")
successful_unrars = 0
files.each do |file|
successful_unrars += 1 if system("unrar x -o+ '#{file}' '#{dir}'")
end
if successful_unrars == files.count
['.pdf','.epub'].each do |ext|
files = Dir.glob("#{escape_glob(dir)}/*#{ext}")
files.each do |file|
if File.basename(file,ext).length <= 9
newname = /^.*\[(.+)\].*$/.match(dir)[1].gsub(/\./,'
').strip.split.map(&:capitalize).join(' ')
puts "renamed to #{newname}"
FileUtils.mv file,"#{destination}/#{newname}#{ext}"
end
end
end
# move to done folder if all rar files unpacked OK
FileUtils.mv dir,moveto
end
end
end
def escape_glob s
s.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
end
def extract file
begin
Zip::File.open(file) do |zip|
zip.each do |zf|
puts "Extracting #{zf.name}"
zf.extract(zf.name) unless File.exists?(zf.name)
end
end
rescue
puts "error unzipping #{file}"
end
end
end
Desyco.new.process_folders(ARGV[0],ARGV[1])
On 2015-05-26 21:32:57 +0000, Tempest said:
> First of all, thanks very much psychopath for your posts, really awesome.
>
> I've made a dumb little ruby script that unzips and unrars Pyscho's
> file structure that others might find useful here.
> The script could easily have been in BASH or anything since it just
> unzips and unrars.
>
> It unzips, then unrars with system() to a nominated folder, then moves
> the processed folder out the way if the unrar was successful - that way
> you can easily see what failed afterwards by what wasn't moved out the
> way.
>
> Very little error checking here but the syntax is:
>
> ruby desyco.rb folders destination
>
> folders can be wildcards, destination is where it unrars to. The
> folders are moved to a folder called "done" in the parent director, you
> need to mkdir that folder before starting.
>
> anyone could easily modify this to also name those files that don't
> have the book name using the books folder name
>
|
|