Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   generate a list of file extensions? (http://www.velocityreviews.com/forums/t885121-generate-a-list-of-file-extensions.html)

davido@codethought.nospamforme.com 02-10-2004 10:43 PM

generate a list of file extensions?
 
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.



David K. Wall 02-10-2004 11:21 PM

Re: generate a list of file extensions?
 
davido@codethought.nospamforme.com <me@privacy.net> 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

davido@codethought.nospamforme.com 02-10-2004 11:34 PM

Re: generate a list of file extensions?
 
On Tue, 10 Feb 2004 23:21:00 -0000, "David K. Wall"
<dwall@fastmail.fm> 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... ;)



Tore Aursand 02-11-2004 01:06 AM

Re: generate a list of file extensions?
 
On Tue, 10 Feb 2004 14:43:49 -0800, davido@codethought.nospamforme.com
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 <tore@aursand.no>
"I am become Death, shatterer of worlds." -- J. Robert Oppenheimer,
upon witnessing the explosion of the first atomic bomb.

Michele Dondi 02-11-2004 02:44 PM

Re: generate a list of file extensions?
 
On Tue, 10 Feb 2004 14:43:49 -0800,
"davido@codethought.nospamforme.com" <me@privacy.net> 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"

davido@codethought.nospamforme.com 02-17-2004 04:44 PM

Re: generate a list of file extensions?
 
On Tue, 17 Feb 2004 15:44:09 +0000, Paul Cooper
<paul.cooper@Nobasspam.ac.uk> wrote:

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


Cygwin is not an option.



Michele Dondi 02-17-2004 10:13 PM

Re: generate a list of file extensions?
 
On Tue, 17 Feb 2004 06:31:09 -0800,
"davido@codethought.nospamforme.com" <me@privacy.net> 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"


All times are GMT. The time now is 09:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57