Getting a list of the files in a directory

Discussion in 'Ruby' started by Revision17, Oct 2, 2003.

  1. Revision17

    Revision17 Guest

    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.
     
    Revision17, Oct 2, 2003
    #1
    1. Advertising

  2. Revision17

    Jim Freeze Guest

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


    --
    Jim Freeze
    ----------
    Hofstadter's Law:
    It always takes longer than you expect, even when you take
    Hofstadter's Law into account.
     
    Jim Freeze, Oct 2, 2003
    #2
    1. Advertising

  3. "Jim Freeze" <> schrieb im Newsbeitrag
    news:...
    > 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_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
     
    Robert Klemme, Oct 2, 2003
    #3
  4. Revision17

    Jim Freeze Guest

    On Thursday, 2 October 2003 at 20:25:44 +0900, Robert Klemme wrote:
    >
    > "Jim Freeze" <> 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
     
    Jim Freeze, Oct 2, 2003
    #4
  5. "Jim Freeze" <> schrieb im Newsbeitrag
    news:...
    > On Thursday, 2 October 2003 at 20:25:44 +0900, Robert Klemme wrote:
    > >
    > > "Jim Freeze" <> 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
     
    Robert Klemme, Oct 2, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    1
    Views:
    517
    Eliyahu Goldin
    Jun 23, 2004
  2. Craig Burkett
    Replies:
    1
    Views:
    6,331
    Bobby Ryzhy
    Aug 10, 2004
  3. Michael McGarry

    Getting a list of files in a directory using a wildcard

    Michael McGarry, Nov 1, 2005, in forum: C Programming
    Replies:
    18
    Views:
    805
    Michael Wojcik
    Nov 5, 2005
  4. Ned White
    Replies:
    4
    Views:
    488
    Eliyahu Goldin
    May 6, 2008
  5. iMath
    Replies:
    8
    Views:
    295
    emile
    Nov 13, 2012
Loading...

Share This Page