generate a list of file extensions?

D

davido

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

David K. Wall

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

davido

Yup.


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... ;)
 
T

Tore Aursand

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
 
M

Michele Dondi

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
 
M

Michele Dondi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top