generate a list of file extensions?

Discussion in 'Perl Misc' started by davido@codethought.nospamforme.com, Feb 10, 2004.

  1. Guest

    I need to scan a directory tree and generate a list of file extensions
    from that tree for a search against a database. That way I can be
    sure the database is properly updated based on all of the extensions
    in this directory. The directory amounts to projects code and binary
    files. I know how to use File::Find to search a list of file types
    and get their *names*. Would searching and getting the file types be
    basically the same thing?

    Could someone point me at some code that could help me accomplish
    this? Much appreciated.
     
    , Feb 10, 2004
    #1
    1. Advertising

  2. <> wrote:

    > I need to scan a directory tree and generate a list of file extensions
    > from that tree for a search against a database. That way I can be
    > sure the database is properly updated based on all of the extensions
    > in this directory. The directory amounts to projects code and binary
    > files. I know how to use File::Find to search a list of file types
    > and get their *names*. Would searching and getting the file types be
    > basically the same thing?


    Yup.

    > Could someone point me at some code that could help me accomplish
    > this? Much appreciated.


    Here's one way:

    use strict;
    use warnings;
    use File::Find;

    my %extensions;

    find(sub {
    $extensions{$1} = '' if /\.([^.]+)$/;
    },
    @list_of_dirs
    );

    print join "\n", keys %extensions;

    I'm not sure what you want to do if the file has no extension....

    --
    David Wall
     
    David K. Wall, Feb 10, 2004
    #2
    1. Advertising

  3. Guest

    On Tue, 10 Feb 2004 23:21:00 -0000, "David K. Wall"
    <> wrote:

    >Yup.
    >
    >> Could someone point me at some code that could help me accomplish
    >> this? Much appreciated.

    >
    >Here's one way:
    >


    Thank you!! It's always forming the regexp that bites me in the
    butt.. ;)

    > use strict;
    > use warnings;
    > use File::Find;
    >
    > my %extensions;
    >
    > find(sub {
    > $extensions{$1} = '' if /\.([^.]+)$/;
    > },
    > @list_of_dirs
    > );
    >
    > print join "\n", keys %extensions;
    >
    >I'm not sure what you want to do if the file has no extension....


    If someone tries that, that's their problem... ;)
     
    , Feb 10, 2004
    #3
  4. Tore Aursand Guest

    On Tue, 10 Feb 2004 14:43:49 -0800,
    wrote:
    > I need to scan a directory tree and generate a list of file extensions
    > from that tree for a search against a database.


    perldoc File::Find
    perldoc File::Basename


    --
    Tore Aursand <>
    "I am become Death, shatterer of worlds." -- J. Robert Oppenheimer,
    upon witnessing the explosion of the first atomic bomb.
     
    Tore Aursand, Feb 11, 2004
    #4
  5. On Tue, 10 Feb 2004 14:43:49 -0800,
    "" <> wrote:

    >I need to scan a directory tree and generate a list of file extensions
    >from that tree for a search against a database. That way I can be
    >sure the database is properly updated based on all of the extensions
    >in this directory. The directory amounts to projects code and binary
    >files. I know how to use File::Find to search a list of file types
    >and get their *names*. Would searching and getting the file types be
    >basically the same thing?
    >
    >Could someone point me at some code that could help me accomplish
    >this? Much appreciated.


    Sorry to say this, but it may be that Perl is not strictly necessary
    for this task, and neither convenient. Even if I use Perl to do almost
    everything[*], when I need to get a list of all file extensions in a
    directory tree I simply issue:

    find whatever/ -type f | sed 's/.*\.//' | sort | uniq

    If you want a Perl only solution, others have given you good hints,
    and of course it quickly becomes necessary when there are enough other
    details you must take care of.


    [*]As I said in another thread some time ago, even to do simple sums
    and other calculations, instead of using bc or bash's own features.


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Feb 11, 2004
    #5
  6. Guest

    On Tue, 17 Feb 2004 15:44:09 +0000, Paul Cooper
    <> wrote:

    >Not so. The excellent Cygwin software provides most of the Unix
    >utilities in a Win32 environment.


    Cygwin is not an option.
     
    , Feb 17, 2004
    #6
  7. On Tue, 17 Feb 2004 06:31:09 -0800,
    "" <> wrote:

    >>Sorry to say this, but it may be that Perl is not strictly necessary
    >>for this task, and neither convenient.

    >
    >Actually, yes it is. This code must run in Win32, Linux and Tru64.
    >The suggestion you made, while excellent, leaves Win32 in the cold.


    Well, it was just an aside... as an aside to the aside, since cygwin
    is not an option as you clearly state in another post, maybe it is
    worth to point out that there do exist "native" ports of *nix
    utilities to win32 platforms. I have UNXUTILS/UNXUPDATES.

    Said this, if it has to be in perl, then you already got good answers,
    but just as a reminder, File::Find & hashes are your (good) friends!

    It can still be a (sort of fat) one-liner:


    C:\TEMP>perl -lMFile::Find -e "find sub { return unless -f and
    s/.*\.//; $a{$_}++ }, shift; print for sort keys %a" stuff\tmp2
    GIF
    JPG
    MPG
    PPT
    bz2
    css
    gif
    gz
    htm
    html
    ion
    jpeg
    jpg
    js
    log
    mp3
    mpg
    pdf
    txt
    zip


    [Note that the script is all on one line: returns are artifacts both
    of the DOS console and of my newsreader]

    But for anything even slightly more complex, please do yourself a
    favour and write a proper warnings- and strict-enabled script...


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Feb 17, 2004
    #7
    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. thewhizkid

    Help! list all file extensions

    thewhizkid, Nov 19, 2003, in forum: Perl
    Replies:
    1
    Views:
    465
    J├╝rgen Exner
    Nov 19, 2003
  2. Weng Tianxiang
    Replies:
    5
    Views:
    1,354
    Christophe
    Feb 16, 2006
  3. Stephen Boulet
    Replies:
    1
    Views:
    264
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Oct 19, 2003
  4. Replies:
    3
    Views:
    440
    red floyd
    Apr 7, 2006
  5. Harlan Messinger
    Replies:
    2
    Views:
    2,354
    John Bell
    Mar 28, 2010
Loading...

Share This Page