source: trunk/tools/shorten-filenames.pl @ 1171

Last change on this file since 1171 was 1171, checked in by pfusik, 11 years ago

shorten-filenames: verify that extensions have between one and three characters.

File size: 741 bytes
Line 
1#!/usr/bin/perl
2use strict;
3
4my $n;
5
6sub process_dir($) {
7        my ($d) = @_;
8        opendir my $h, $d or die "$d: $!\n";
9        my @e = sort grep !/^\./, readdir $h;
10        closedir $h;
11        my %h;
12        if (@e > 64) {
13                printf "%s: %d entries\n", $d, scalar @e;
14        }
15        for my $e (@e) {
16                my $l = "$d/$e";
17                my $s = uc $e;
18                $s =~ y/-_//d;
19                my ($f, $x) = $s =~ /^([0-9A-Z]+)(\.[0-9A-Z]{1,3})?$/ or die "$l: illegal filename\n";
20                $f = substr($f, 0, $n);
21                my $c = 1;
22                while ($h{$f.$x}++) {
23                        ++$c;
24                        $f = substr($f, 0, $n - length($c)) . $c;
25                }
26                printf "%-*s%-5s%s\n", $n, $f, $x, $l;
27                if (-d $l) {
28                        process_dir($l);
29                }
30        }
31}
32
33if (@ARGV != 2 || $ARGV[0] !~ /^\d\d?$/) {
34        die "Usage: perl shorten-filenames.pl NUMCHARS DIR\n";
35}
36$n = $ARGV[0];
37process_dir($ARGV[1]);
Note: See TracBrowser for help on using the repository browser.