librelist archives

« back to archive

How to list all JPG/jpg files in a folder

How to list all JPG/jpg files in a folder

From:
Filemon Salas
Date:
2012-01-08 @ 13:45
Hi,

I'm currently working on a small program to rename my pictures and I'm
having a hard time trying to figure some thing out.

What I have is the following code which basically returns the name and
path for each picture in the specified folder (FolderName).

pic_names = Dir['/Users/UserName/Desktop/FolderName/*.{JPG,jpg}']

This code works just fine but as you can see its a fix PATH, and I
would like to use something like ask_open_folder to get the path which
can be any directory. In other words the way my program works right
now is that a folder name "FolderName" should exist.

I have tried this....

select_dir = ask_open_folder
pic_names = Dir['select_dir/*.{JPG,jpg}']

But it doesn't work.

Can someone help me with this code?

Thanks a lot

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Khristian Alexander Schönrock
Date:
2012-01-08 @ 13:50
Hi,
I think you should be using #{}  to insert the user chosen path into the 
path given to Dir.
Like this:

pic_names = Dir["#{select_dir}/*.{JPG,jpg}"]

Note that the quotes were also changed to double quotes.

Em 08/01/2012 11:45, Filemon Salas escreveu:
> Hi,
>
> I'm currently working on a small program to rename my pictures and I'm
> having a hard time trying to figure some thing out.
>
> What I have is the following code which basically returns the name and
> path for each picture in the specified folder (FolderName).
>
> pic_names = Dir['/Users/UserName/Desktop/FolderName/*.{JPG,jpg}']
>
> This code works just fine but as you can see its a fix PATH, and I
> would like to use something like ask_open_folder to get the path which
> can be any directory. In other words the way my program works right
> now is that a folder name "FolderName" should exist.
>
> I have tried this....
>
> select_dir = ask_open_folder
> pic_names = Dir['select_dir/*.{JPG,jpg}']
>
> But it doesn't work.
>
> Can someone help me with this code?
>
> Thanks a lot

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Filemon Salas
Date:
2012-01-08 @ 14:00
It worked, I needed to be converted to a string, hmm.

Thanks a lot for your help, you guys are awesome!


2012/1/8 Khristian Alexander Schönrock <der.kosak@gmail.com>:
> Hi,
> I think you should be using #{}  to insert the user chosen path into the
> path given to Dir.
> Like this:
>
> pic_names = Dir["#{select_dir}/*.{JPG,jpg}"]
>
> Note that the quotes were also changed to double quotes.
>
> Em 08/01/2012 11:45, Filemon Salas escreveu:
>> Hi,
>>
>> I'm currently working on a small program to rename my pictures and I'm
>> having a hard time trying to figure some thing out.
>>
>> What I have is the following code which basically returns the name and
>> path for each picture in the specified folder (FolderName).
>>
>> pic_names = Dir['/Users/UserName/Desktop/FolderName/*.{JPG,jpg}']
>>
>> This code works just fine but as you can see its a fix PATH, and I
>> would like to use something like ask_open_folder to get the path which
>> can be any directory. In other words the way my program works right
>> now is that a folder name "FolderName" should exist.
>>
>> I have tried this....
>>
>> select_dir = ask_open_folder
>> pic_names = Dir['select_dir/*.{JPG,jpg}']
>>
>> But it doesn't work.
>>
>> Can someone help me with this code?
>>
>> Thanks a lot

Re: [shoes] How to list all JPG/jpg files in a folder

From:
J. Kaiden
Date:
2012-01-08 @ 19:05
hi Fily,

...needed to be converted to a string...
>

  really you're not converting `select_dir`  *to* a string, but *from* a
string back into the variable it represents...

this:


> >> select_dir = ask_open_folder
> >> pic_names = Dir['select_dir/*.{JPG,jpg}']
>

looks the same to the interpreter as this:

    pic_names = Dir['select_dir' + '/*.{JPG,jpg}']

...take this for example,

    variable = "hello there"
    p variable
    p 'variable'
    p "#{variable}"

    => "hello there"
    => "variable"
    => "hello there"

  in your `pic_names=` line above, the variable `select_dir` is inside of
single quotes ( ' ' ) along with the wildcard and file extensions.

  when you change the code to:

    select_dir = ask_open_folder
    pic_names = Dir["#{select_dir}/*.{JPG,jpg}"]

  ...the #{} method interpolates `select_dir` back into the variable it
represents.

  note that the #{} has to be used with double-quotes ( " " ) or the string
will not be interpolated -

    p '#{variable}'

    => "\#{variable}"


hth -

Shoes On!

  - j

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Filemon Salas
Date:
2012-01-09 @ 01:27
>>when you change the code to:
   >> select_dir = ask_open_folder
   >> pic_names = Dir["#{select_dir}/*.{JPG,jpg}"]
  >>...the #{} method interpolates `select_dir` back into the variable
it represents.
 >> note that the #{} has to be used with double-quotes ( " " ) or the
string will not be >>interpolated

Sorry but now I dont fully understand how exactly this is working I
thought that #{} was the same as to_s but apparently it has some other
functions because I changed my code to "select_dir.to_s/*.{JPG,jpg}"
and it didn't work as you explained it. Perhaps I dont know understand
it because I dont know what interpolation is.

Dont get me wrong the explanation you gave me is clear I just dont
know what interpolation is and how it works.  I appreciate that you
actually reply otherwise I would be thinking that all I did was
converted it to a string.

I guess I will need to read more about interpolation.

Thanks a lot


On Sun, Jan 8, 2012 at 1:05 PM, J. Kaiden <jakekaiden@gmail.com> wrote:
> hi Fily,
>
>> ...needed to be converted to a string...
>
>
>   really you're not converting `select_dir`  *to* a string, but *from* a
> string back into the variable it represents...
>
> this:
>
>>
>> >> select_dir = ask_open_folder
>> >> pic_names = Dir['select_dir/*.{JPG,jpg}']
>
>
> looks the same to the interpreter as this:
>
>     pic_names = Dir['select_dir' + '/*.{JPG,jpg}']
>
> ...take this for example,
>
>     variable = "hello there"
>     p variable
>     p 'variable'
>     p "#{variable}"
>
>     => "hello there"
>     => "variable"
>     => "hello there"
>
>   in your `pic_names=` line above, the variable `select_dir` is inside of
> single quotes ( ' ' ) along with the wildcard and file extensions.
>
>   when you change the code to:
>
>     select_dir = ask_open_folder
>     pic_names = Dir["#{select_dir}/*.{JPG,jpg}"]
>
>   ...the #{} method interpolates `select_dir` back into the variable it
> represents.
>
>   note that the #{} has to be used with double-quotes ( " " ) or the string
> will not be interpolated -
>
>     p '#{variable}'
>
>     => "\#{variable}"
>
>
> hth -
>
> Shoes On!
>
>   - j

Re: [shoes] How to list all JPG/jpg files in a folder

From:
J. Kaiden
Date:
2012-01-09 @ 02:35
hi Fily,


> Sorry but now I dont fully understand how exactly this is working
>


 no problem - this stuff gets kind of confusing sometimes ;)

I thought that #{} was the same as to_s
>

 they are similar, as the #{} operator interprets whatever is inside it and
returns the result as a string - but in some ways you can almost think of
them as opposites..

  `to_s` will convert some object (an array or an integer, for example)
into a string:

    array = [1, "tiddlywinks", 3.14]
    p array.to_s

    => "1tiddlywinks3.14"

  ...whereas the `#{}` operator gives you access to a variable or operation
from within the double quotes marking a string - check this out:

    person = "jk"
    favorite_day = "wednesday"
    favorite_food = "clam chowder"

    puts "hi there #{person},
    since today is #{favorite_day}
    why don't we have some #{favorite_food}?"

  ...vs...

    puts "hi there person.to_s,
    since today is favorite_day.to_s
    why don't we have some favorite_food.to_s?"

  ...see the difference?  the `to_s` call is inside of quotes, and so is
not recognized as a method, but rather just part of the string.

  also, having assigned the variable `favorite_food` to "clam chowder, "
makes `favorite_food` a string, so calling `to_s` on it doesn't do too much.

  if `favorite_food` were assigned the integer 38 (a strange favorite food,
i know,) then the #{} operator would fetch the variable (38) and call
`to_s` on it - so that the last line would read:

    why don't we have some 38?

  `to_s` can be very useful when for example you're displaying text in a
gui and need to convert integers to strings, and the converse - `to_i` -
can be very useful for example for turning numbers read from a text file
into integers to be used in calculations...


  happy Shoes'in

  - j

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Filemon Salas
Date:
2012-01-09 @ 14:49
Thank you all a lot for your help! You guys are awesome!

On Sun, Jan 8, 2012 at 8:35 PM, J. Kaiden <jakekaiden@gmail.com> wrote:
> hi Fily,
>
>>
>> Sorry but now I dont fully understand how exactly this is working
>
>
>
>  no problem - this stuff gets kind of confusing sometimes ;)
>
>> I thought that #{} was the same as to_s
>
>
>  they are similar, as the #{} operator interprets whatever is inside it and
> returns the result as a string - but in some ways you can almost think of
> them as opposites..
>
>   `to_s` will convert some object (an array or an integer, for example) into
> a string:
>
>     array = [1, "tiddlywinks", 3.14]
>     p array.to_s
>
>     => "1tiddlywinks3.14"
>
>   ...whereas the `#{}` operator gives you access to a variable or operation
> from within the double quotes marking a string - check this out:
>
>     person = "jk"
>     favorite_day = "wednesday"
>     favorite_food = "clam chowder"
>
>     puts "hi there #{person},
>     since today is #{favorite_day}
>     why don't we have some #{favorite_food}?"
>
>   ...vs...
>
>     puts "hi there person.to_s,
>     since today is favorite_day.to_s
>     why don't we have some favorite_food.to_s?"
>
>   ...see the difference?  the `to_s` call is inside of quotes, and so is not
> recognized as a method, but rather just part of the string.
>
>   also, having assigned the variable `favorite_food` to "clam chowder, "
> makes `favorite_food` a string, so calling `to_s` on it doesn't do too much.
>
>   if `favorite_food` were assigned the integer 38 (a strange favorite food,
> i know,) then the #{} operator would fetch the variable (38) and call `to_s`
> on it - so that the last line would read:
>
>     why don't we have some 38?
>
>   `to_s` can be very useful when for example you're displaying text in a gui
> and need to convert integers to strings, and the converse - `to_i` - can be
> very useful for example for turning numbers read from a text file into
> integers to be used in calculations...
>
>
>   happy Shoes'in
>
>   - j
>
>

Re: [shoes] How to list all JPG/jpg files in a folder

From:
J. Kaiden
Date:
2012-01-09 @ 21:17
> Thank you all a lot for your help! You guys are awesome!


  no problem - and as Peter says, don't be afraid to ask questions - we're
all learning something new (or trying to!) every day...

  Shoes Away....

  - j

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Peter Fitzgibbons
Date:
2012-01-09 @ 02:02
HI Fily,

Interpolation is a way Ruby can add strings together.

In old Java or C world (works in Ruby this way too), strings could be added
:

"Hello " + "World"
=> "Hello World"

We can also make a variable hold part of the string :
greeting = "Hello"
=> "Hello"

Then use interpolation to add things together :
"#{greeting} World"
=> "Hello World"

Note that interpolation is VERY powerful, and can hold just about *any*
Ruby statement, so crazy stuff can be done with string-interpolation.

Also note that .to_s only works on a variable *outside* of a quote, so :

"greeting.to_s"  # <-- quotes without interpolation
=> "greeting.to_s"

greeting.to_s  # <-- regular call to #to_s
=> "Hello"

Here's a completely redundant interpolation of #to_s, since greeting... is
already a string!
"#{greeting.to_s} World"
=> "Hello World"


IRB is a very good way to get a quick environment to test these out.

I hope that helps a little?

Regards,
Peter Fitzgibbons
(847) 859-9550
Email: peter.fitzgibbons@gmail.com
IM GTalk: peter.fitzgibbons
IM AOL: peter.fitzgibbons@gmail.com

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Fily
Date:
2012-01-09 @ 02:24
Ok, I think I got it.  Shame on me I dont know how I didnt get it the 
first time, I think I confused my self thinking that I was converting and 
by not knowing or remembering what interpolation was, it is now clear and 
now I know what interpolation is, it feels good when thing start making 
sense.

Thanks a LOT

Sent from my iPhone

On Jan 8, 2012, at 8:02 PM, Peter Fitzgibbons <peter.fitzgibbons@gmail.com> wrote:

> HI Fily,
> 
> Interpolation is a way Ruby can add strings together.
> 
> In old Java or C world (works in Ruby this way too), strings could be added :
> 
> "Hello " + "World"
> => "Hello World"
> 
> We can also make a variable hold part of the string :
> greeting = "Hello"
> => "Hello"
> 
> Then use interpolation to add things together :
> "#{greeting} World"
> => "Hello World"
> 
> Note that interpolation is VERY powerful, and can hold just about *any* 
Ruby statement, so crazy stuff can be done with string-interpolation.
> 
> Also note that .to_s only works on a variable *outside* of a quote, so :
> 
> "greeting.to_s"  # <-- quotes without interpolation
> => "greeting.to_s"
> 
> greeting.to_s  # <-- regular call to #to_s
> => "Hello"
> 
> Here's a completely redundant interpolation of #to_s, since greeting... 
is already a string!
> "#{greeting.to_s} World"
> => "Hello World"
> 
> 
> IRB is a very good way to get a quick environment to test these out.
> 
> I hope that helps a little?
> 
> Regards,
> Peter Fitzgibbons
> (847) 859-9550
> Email: peter.fitzgibbons@gmail.com
> IM GTalk: peter.fitzgibbons
> IM AOL: peter.fitzgibbons@gmail.com
> 
> 

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Peter Fitzgibbons
Date:
2012-01-09 @ 03:27
You're welcome!

NO SHAME!  We're all learning something new every single day!   Even old
dogs like me!

Peter Fitzgibbons
(847) 859-9550
Email: peter.fitzgibbons@gmail.com
IM GTalk: peter.fitzgibbons
IM AOL: peter.fitzgibbons@gmail.com

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Steve Klabnik
Date:
2012-01-08 @ 16:03
> It worked, I needed to be converted to a string, hmm.

#{} automatically calls to_s on what's inside. Its one of the reasons
to prefer #{} over +.

Re: [shoes] How to list all JPG/jpg files in a folder

From:
Fily
Date:
2012-01-08 @ 18:24
Thank you for your reply!

I agree, I just didnt know I needed to convert it.

Thanks

On Jan 8, 2012, at 10:03 AM, Steve Klabnik <steve@steveklabnik.com> wrote:

>> It worked, I needed to be converted to a string, hmm.
> 
> #{} automatically calls to_s on what's inside. Its one of the reasons
> to prefer #{} over +.