[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting a list of the files in a directory

revision17

10/2/2003 3:01:00 AM

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...), 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.
5 Answers

Mark J. Reed

10/2/2003 3:04:00 AM

0

On Wed, Oct 01, 2003 at 08:01:21PM -0700, Revision17 wrote:
> 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...), 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?

It's in the Dir class, not the File class, which is why you didn't
see it. :)

The easiest way is probably to use foreach and do your renaming in
the block you pass to it:

Dir.foreach(pathname) do
|f|
# do whatever you want with f, which is a filename within the
# given directory (not fully-qualified)
end

Jim Freeze

10/2/2003 10:19:00 AM

0

On Thursday, 2 October 2003 at 12:24:25 +0900, Revision17 wrote:
> 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...), 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.


--
Jim Freeze
----------
Hofstadter's Law:
It always takes longer than you expect, even when you take
Hofstadter's Law into account.

Robert Klemme

10/2/2003 11:20:00 AM

0


"Jim Freeze" <jim@freeze.org> schrieb im Newsbeitrag
news:20031002062017.A50661@freeze.org...
> On Thursday, 2 October 2003 at 12:24:25 +0900, Revision17 wrote:
> > 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...), 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

Jim Freeze

10/2/2003 1:58:00 PM

0

On Thursday, 2 October 2003 at 20:25:44 +0900, Robert Klemme wrote:
>
> "Jim Freeze" <jim@freeze.org> schrieb im Newsbeitrag
> >
> > 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. :)


--
Jim Freeze
----------
Every little picofarad has a nanohenry all its own.
-- Don Vonada

Robert Klemme

10/2/2003 3:44:00 PM

0


"Jim Freeze" <jim@freeze.org> schrieb im Newsbeitrag
news:20031002095840.A51014@freeze.org...
> On Thursday, 2 October 2003 at 20:25:44 +0900, Robert Klemme wrote:
> >
> > "Jim Freeze" <jim@freeze.org> schrieb im Newsbeitrag
> > >
> > > 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