Question

I want to shuffle thousands of files into many directories like this.

require 'fileutils'
files = Dir.glob("**/*.jpg")
files.shuffle!
((files.size/100)+1).times do |i|
  FileUtils.mkdir_p("%03d" % i)
  100.times{|j|
    begin
      FileUtils.mv(files[j+i*100],"%03d" % i)
    rescue ArgumentError
    end
    }
end

Sometimes source and target are same file. To ignore the error I'm using rescue ArgumentError.

But I think it's bad way to use Exception like this. Is there better way to do nothing when source and target are same?

Was it helpful?

Solution

orig, dest = files[j+i*100], "%03d" % i
FileUtils.mv(orig, dest) unless File.basename(orig) == dest
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top