orig, dest = files[j+i*100], "%03d" % i
FileUtils.mv(orig, dest) unless File.basename(orig) == dest
How to ignore error if source and target is same when using `FileUtils.mv` in ruby
Domanda
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?
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow