en cours de lecture Programme d'apprentissage. Im à la page 91-92 où vous créez un programme qui déplace des images de votre lecteur USB vers l'emplacement désiré et change le nom de chaque image. Mais je reçois l'erreur suivante lors de l'exécution du programme. En utilisant Ubuntu comme vous pouvez le dire, mais obtenez "Lien multi-périphérique non valide". Des idées de comment résoudre cela?Déplacement d'une image vers un autre dossier
[email protected]:~/ruby$ ruby move.rb
What would you like to call this batch?
IMG
Downloading 1 files: .move.rb:36:in `rename': Invalid cross-device link - (/media/SanDisk Cruzer Blade/pictures/UMG.jpg, IMG01.jpg) (Errno::EXDEV)
from move.rb:36:in `block in <main>'
from move.rb:17:in `each'
from move.rb:17:in `<main>'
Ce code
# Heres where the pictures are stored
Dir.chdir '/home/pierre/Skrivbord'
# First we find all of the pictures to be moved
pic_names = Dir['/media/SanDisk Cruzer Blade/pictures/**/*.{JPG,jpg}']
puts 'What would you like to call this batch?'
batch_name = gets.chomp
puts
print "Downloading #{pic_names.length} files: "
# This will be our counter. We'll start at 1 today,
# though normally I like to count from 0.
pic_number = 1
pic_names.each do |name|
print '.' # This is our "progress bar".
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end
# This renames the picture, but since "name" has a big long
# path on it, and "new_name" doesn't, it also moves the file to the current
# working directory, which is now Katy's PictureInbox folder. Since it's a
# *move*, this effectively downloads and deletes the originals. And since this
# is a memory card, not a hard drive, each of these takes a second or so; hence,
# the little dots let her know that my program didn't hose her machine.
# (Some marriage advice from your favourite author/programmer: it's all about
# the little things.)
# Now where were we? Oh, yeah...
File.rename name, new_name
# Finally, we increment the counter.
pic_number = pic_number + 1
end
puts # This is so we aren't on progress bar line.
puts 'Done, cutie!'
Pierre, je suppose que vous ne pouvez pas renommer un fichier si ce n'est pas sur le même système de fichiers. Vous devez déplacer le fichier, pas le renommer en utilisant FileUtils. – boretom