Problems moving and renaming pictures with this code, what wrong?

F

Fily Salas

Hi,

I have been reading the book called =E2=80=9CLearning to Program=E2=80=9D=
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=E2=80=99t work.

I have some .jpg pictures located in a folder called =E2=80=9Ctemp-photos=
=E2=80=9D 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 =E2=80=9Cmy-photos=E2=
=80=9D and I=E2=80=99m
trying the code below but when I run it, it just doesn=E2=80=99t do anyth=
ing, no
errors but the pictures don=E2=80=99t get moved or renamed.

Any idea what am I doing wrong? I don=E2=80=99t actually need this progr=
am but
since I=E2=80=99m practicing I want to understand what=E2=80=99s going on=
 
A

Anurag Priyam

I have some .jpg pictures located in a folder called =93temp-photos=94 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 =93my-photos=94 and = I=92m
trying the code below but when I run it, it just doesn=92t do anything, n= o
errors but the pictures don=92t get moved or renamed. [...]
pic_names =3D Dir['/Users/userName/Desktop/temp-photos.{JPG,jpg}']

Since temp-photos is a directory, should not the glob pattern be:

pic_names =3D Dir['/Users/userName/Desktop/temp-photos/*.{JPG,jpg}']

--=20
Anurag Priyam
http://about.me/yeban/
 
A

Albert Schlef

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top