A collection of maintenance scripts for the Sciveyor database.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

78 lines
1.9 KiB

#!/usr/bin/env ruby
# Check to make sure that there is a "complete set" of extensions for each
# filename in this folder and all sub-folders (rescursively). Call this script
# with all the extensions you expect to find, and it will put any files with
# some extensions missing in a sub-folder called 'orphans'. E.g.,
#
# folder_extensions pdf,txt,xml
#
# will find any file for which there are only pdf and xml versions and place
# the pdf and xml into the 'orphans' sub-folder.
require 'pathname'
require 'fileutils'
require 'ruby-progressbar'
$extensions = ARGV.shift.split(',')
$extensions.map! { |ext| ext.start_with?('.') ? ext : '.' + ext }
def process_directory(dir)
puts "...scanning directory #{dir}"
files = Pathname.glob(dir / '*')
directories = []
uniques = []
puts "...generating unique files for #{dir}"
files.each do |f|
if f.directory?
directories << f
else
uniques << f.dirname.join(f.basename('.*'))
end
end
uniques.uniq!
progress =
ProgressBar.create(
format: '%a [%B] %e %j%% %t',
title: "Processing '#{dir}'",
total: uniques.size
)
uniques.each do |f|
unless f
progress.increment
next
end
$extensions.each do |e|
check = f.sub_ext(e)
unless files.include?(check)
pattern = f.sub_ext('.*')
to_move = files.select { |t| t.fnmatch(pattern.to_s) }
if to_move.empty?
progress.log "...ERROR: looking for orphan #{f} and found nothing?"
else
dirname = f.dirname
orphans = dirname.join('orphans')
FileUtils.mkdir_p orphans
FileUtils.mv to_move, orphans
progress.log "...#{f} is an orphan, moved extensions #{
to_move.map { |t| t.extname }
}!"
end
break
end
end
progress.increment
end
directories.each { |dir| process_directory(dir) }
end
process_directory(Pathname.new('.'))