Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > generate a list of file extensions?

Reply
Thread Tools

generate a list of file extensions?

 
 
davido@codethought.nospamforme.com
Guest
Posts: n/a
 
      02-10-2004
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.


 
Reply With Quote
 
 
 
 
David K. Wall
Guest
Posts: n/a
 
      02-10-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
davido@codethought.nospamforme.com
Guest
Posts: n/a
 
      02-10-2004
On Tue, 10 Feb 2004 23:21:00 -0000, "David K. Wall"
<(E-Mail Removed)> 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...


 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      02-11-2004
On Tue, 10 Feb 2004 14:43:49 -0800, (E-Mail Removed)
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 <(E-Mail Removed)>
"I am become Death, shatterer of worlds." -- J. Robert Oppenheimer,
upon witnessing the explosion of the first atomic bomb.
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      02-11-2004
On Tue, 10 Feb 2004 14:43:49 -0800,
"(E-Mail Removed)" <(E-Mail Removed)> 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"
 
Reply With Quote
 
davido@codethought.nospamforme.com
Guest
Posts: n/a
 
      02-17-2004
On Tue, 17 Feb 2004 15:44:09 +0000, Paul Cooper
<(E-Mail Removed)> wrote:

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


Cygwin is not an option.


 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      02-17-2004
On Tue, 17 Feb 2004 06:31:09 -0800,
"(E-Mail Removed)" <(E-Mail Removed)> 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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed to generate a user instance of SQL Server. Only an integratedconnection can generate a user instance. Harlan Messinger ASP .Net 2 03-28-2010 06:51 PM
how to read parse data from two file to generate one one file Asif Iqbal Ruby 0 08-06-2009 04:47 PM
Generate .aspx file and .cs file behind it jyoti.bannigidad@gmail.co Software 0 09-18-2008 06:54 AM
How to generate warnings when How generate a warning when int is converted to bool or vice versa? PengYu.UT@gmail.com C++ 3 04-06-2006 11:24 PM
How to generate variable labels for same component within a generate loop Weng Tianxiang VHDL 5 02-16-2006 01:45 PM



Advertisments