Hi,
I have been reading the book called “Learning to Program” and I have to
say it, for beginners this is a great book, anyways, while practicing
the exercises found in the book I came across this code that for some
reason it doesn’t work.
I have some .jpg pictures located in a folder called “temp-photos” in my
desktop and I want to rename them and then move them to a folder which
is also located in my desktop but in a folder called “my-photos” and I’m
trying the code below but when I run it, it just doesn’t do anything, no
errors but the pictures don’t get moved or renamed.
Any idea what am I doing wrong? I don’t actually need this program but
since I’m practicing I want to understand what’s going on.
Thanks
···
----------------------------------------------
Dir.chdir '/Users/userName/Desktop/my-photos'
pic_names = Dir['/Users/userName/Desktop/temp-photos.{JPG,jpg}']
puts 'What would you like to call this batch?'
batch_name = gets.chomp
puts
print "Downloading #{pic_names.length} files: "
pic_number = 1
pic_names.each do |name|
print '.'
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpeg"
else
"#{batch_name}#{pic_number}.jpeg"
end
File.rename name, new_name
pic_number = pic_number + 1
end
puts
puts 'Done'
--
Posted via http://www.ruby-forum.com/.
I have some .jpg pictures located in a folder called “temp-photos” in my
desktop and I want to rename them and then move them to a folder which
is also located in my desktop but in a folder called “my-photos” and I’m
trying the code below but when I run it, it just doesn’t do anything, no
errors but the pictures don’t get moved or renamed.
[...]
pic_names = Dir['/Users/userName/Desktop/temp-photos.{JPG,jpg}']
Since temp-photos is a directory, should not the glob pattern be:
pic_names = Dir['/Users/userName/Desktop/temp-photos/*.{JPG,jpg}']
···
--
Anurag Priyam
http://about.me/yeban/
I know, shame on me I should know that 
Thanks a lot for your help!
···
--
Posted via http://www.ruby-forum.com/.
Fily Salas wrote in post #988915:
I want to rename them and then move them
You can do this in one step.
pic_number = 1
pic_names.each do |name|
[...]
pic_number = pic_number + 1
end
You can instead do:
pic_names.each_with_index do |name, pic_number|
[...]
end
(but pic_number would start at 0.)
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end
You can also do:
new_name = "%s%0d2.jpg" % [batch_name, pic_number+1]
(I added 1 to pic_number because now it starts at zero.)
···
--
Posted via http://www.ruby-forum.com/\.
Thank you for the simplified version.
···
--
Posted via http://www.ruby-forum.com/.