1: This code came from my desire to use perl for something
2: besides CGI, which is my current perl-y focus.
3: I expected slightly different behavior from
4: File::Find::file, though this may be my Win background
5: shining through.
6: Put simply, this will traverse a filesystem, and determine
7: the # of files based on extention. Again, this was more to
8: keep the programming muscles limber; I plan on using this as a shell for more complex things. I do appreciate any
9: comments you have, however.
10: <code>
11: #use warnings;
12: use strict;
13: use File::Find;
14:
15: my %hash;
16: my @temp;
17:
18: File::Find::find (\&wanted,"c:\\");
19: my @keys = keys (%hash);
20: @keys = sort_by_value (\%hash);
21:
22:
23: print "-"x20,"\n";
24: foreach my $key(@keys) {if ($key =~ /^$/) {print "no ext\t:\t"}else{ print "$key\t:\t"}; print $hash{$key},"\n"}
25:
26: sub sort_by_value {
27: my %hash = %{shift()};
28: sort {$hash {$a} <=> $hash{$b} } keys %hash;
29: }
30:
31:
32: sub wanted {
33: my @file ;
34: my $fname;
35: # don't attempt to read a directory.
36: if (-d $_) {return};
37: $fname = $File::Find::name;
38: # turn any '/' into a '\'
39: # then make sure that any '\\' get turned into '\'
40: $fname =~ s#\/#\\#g;
41: $fname =~ s#\\\\#\\#;
42: @temp= split /\./, $_;
43: # mmm. autovivification
44: $hash{@temp[1]}++
45: }
46: </code>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: file ext counter.
by chromatic (Archbishop) on Nov 23, 2000 at 10:35 UTC | |
|
Re: file ext counter.
by lemming (Priest) on Nov 22, 2000 at 21:17 UTC | |
by boo_radley (Parson) on Nov 22, 2000 at 21:25 UTC |