Getting a list of the files in a directory

R

Revision17

Hi, I'm just starting out with ruby and I'm writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I've looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can't
seem to find the method I'd use to get all the names of files in a
directory. How can I do this?

Sorry about the ultra simplitic question.
 
J

Jim Freeze

Hi, I'm just starting out with ruby and I'm writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I've looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can't
seem to find the method I'd use to get all the names of files in a
directory. How can I do this?

The easy way is to use the glogging feature of Dir#[].
For example

files = Dir["*"]

To upcase all files in a directory, do:

Dir["*"].each { |file| `mv #{file} #{file.upcase}` }

for unix. It's similar for windows, but I don't have
a windows box to test it.
 
R

Robert Klemme

Jim Freeze said:
Hi, I'm just starting out with ruby and I'm writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I've looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can't
seem to find the method I'd use to get all the names of files in a
directory. How can I do this?

The easy way is to use the glogging feature of Dir#[].
For example

files = Dir["*"]

To upcase all files in a directory, do:

Dir["*"].each { |file| `mv #{file} #{file.upcase}` }

for unix. It's similar for windows, but I don't have
a windows box to test it.

Why not

Dir["*"].each { |file| File.rename( file, file.upcase ) }

?

robert
 
J

Jim Freeze

Jim Freeze said:
The easy way is to use the glogging feature of Dir#[].
For example

files = Dir["*"]

To upcase all files in a directory, do:

Dir["*"].each { |file| `mv #{file} #{file.upcase}` }

for unix. It's similar for windows, but I don't have
a windows box to test it.

Why not

Dir["*"].each { |file| File.rename( file, file.upcase ) }

That works to. :)
 
R

Robert Klemme

Jim Freeze said:
Jim Freeze said:
The easy way is to use the glogging feature of Dir#[].
For example

files = Dir["*"]

To upcase all files in a directory, do:

Dir["*"].each { |file| `mv #{file} #{file.upcase}` }

for unix. It's similar for windows, but I don't have
a windows box to test it.

Why not

Dir["*"].each { |file| File.rename( file, file.upcase ) }

That works to. :)

:)))

.... and it's portable!

robert
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top