Removing file names with '.' in their names from list?

Discussion in 'Ruby' started by Sfdesigner Sfdesigner, Aug 12, 2007.

  1. Currently I'm using this code to output a list of files to a .rhtml
    page:

    <%
    Dir.foreach("/") do |file|
    next if File.fnmatch('*.*', file)
    puts '<a href="file">' + file + '</a>'
    end
    %>

    This removes files from the list output such as 'photo.jpg'.

    But it leaves the files with names like '.htaccess', ',' and '..'.

    I could create more of the 'next' statements explicitly for those file
    and directory names but wondering if there's a more elegant way to say
    don't output files with a '.' in their names.
    --
    Posted via http://www.ruby-forum.com/.
     
    Sfdesigner Sfdesigner, Aug 12, 2007
    #1
    1. Advertising

  2. Sfdesigner Sfdesigner

    Robert Dober Guest

    On 8/12/07, Sfdesigner Sfdesigner <> wrote:
    > Currently I'm using this code to output a list of files to a .rhtml
    > page:
    >
    > <%
    > Dir.foreach("/") do |file|
    > next if File.fnmatch('*.*', file)

    I would suggest
    next if /\./ === file
    > puts '<a href="file">' + file + '</a>'
    > end
    > %>
    >

    HTH
    Robert
    --
    [...] as simple as possible, but no simpler.
    -- Attributed to Albert Einstein
     
    Robert Dober, Aug 13, 2007
    #2
    1. Advertising

  3. Sfdesigner Sfdesigner, Aug 13, 2007
    #3
  4. Sfdesigner Sfdesigner

    Ken Bloom Guest

    On Mon, 13 Aug 2007 07:16:49 +0900, Sfdesigner Sfdesigner wrote:

    > Currently I'm using this code to output a list of files to a .rhtml
    > page:
    >
    > <%
    > Dir.foreach("/") do |file|
    > next if File.fnmatch('*.*', file)
    > puts '<a href="file">' + file + '</a>'
    > end
    > %>
    >
    > This removes files from the list output such as 'photo.jpg'.
    >
    > But it leaves the files with names like '.htaccess', ',' and '..'.
    >
    > I could create more of the 'next' statements explicitly for those file
    > and directory names but wondering if there's a more elegant way to say
    > don't output files with a '.' in their names.


    Under UNIX, a * never matches a . at the beginning of a filename, and *.*
    also never matches this.

    add another rule
    next if File.fnmatch('.*',file)
    and you should be fine.

    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
     
    Ken Bloom, Aug 13, 2007
    #4
  5. Hi,

    At Mon, 13 Aug 2007 07:16:49 +0900,
    Sfdesigner Sfdesigner wrote in [ruby-talk:264328]:
    > Currently I'm using this code to output a list of files to a .rhtml
    > page:
    >
    > <%
    > Dir.foreach("/") do |file|
    > next if File.fnmatch('*.*', file)
    > puts '<a href="file">' + file + '</a>'
    > end
    > %>
    >
    > This removes files from the list output such as 'photo.jpg'.
    >
    > But it leaves the files with names like '.htaccess', ',' and '..'.


    File.fnmatch('*.*', file, File::FNM_DOTMATCH)

    --
    Nobu Nakada
     
    Nobuyoshi Nakada, Aug 13, 2007
    #5
  6. Sfdesigner Sfdesigner

    Chris Shea Guest

    On Aug 12, 4:16 pm, Sfdesigner Sfdesigner
    <> wrote:
    > Currently I'm using this code to output a list of files to a .rhtml
    > page:
    >
    > <%
    > Dir.foreach("/") do |file|
    > next if File.fnmatch('*.*', file)
    > puts '<a href="file">' + file + '</a>'
    > end
    > %>
    >
    > This removes files from the list output such as 'photo.jpg'.
    >
    > But it leaves the files with names like '.htaccess', ',' and '..'.
    >
    > I could create more of the 'next' statements explicitly for those file
    > and directory names but wondering if there's a more elegant way to say
    > don't output files with a '.' in their names.
    > --
    > Posted viahttp://www.ruby-forum.com/.


    perhaps:
    next if file.include?('.')

    Though Robert's solution does the same thing.

    HTH,
    Chris
     
    Chris Shea, Aug 13, 2007
    #6
    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. Ashraf Fouad
    Replies:
    2
    Views:
    564
    Ashraf Fouad
    Jan 8, 2004
  2. Michael Krasnyk

    Print variable values by their names

    Michael Krasnyk, Oct 18, 2004, in forum: Python
    Replies:
    1
    Views:
    279
    Peter Otten
    Oct 18, 2004
  3. Noah
    Replies:
    2
    Views:
    273
  4. Tomas PAJER
    Replies:
    4
    Views:
    165
    Patrice
    Nov 28, 2005
  5. Sandman

    input names with '-' in their names.

    Sandman, Sep 3, 2003, in forum: Javascript
    Replies:
    4
    Views:
    110
    Lasse Reichstein Nielsen
    Sep 4, 2003
Loading...

Share This Page