Mercurial > hg > chronicle
comparison bin/check-titles @ 227:9e79fe60ad47
Added to repository
author | Steve Kemp <steve@steve.org.uk> |
---|---|
date | Fri, 02 May 2008 21:04:45 +0100 |
parents | |
children | a9e686d7f419 |
comparison
equal
deleted
inserted
replaced
226:37668661af76 | 227:9e79fe60ad47 |
---|---|
1 #!/usr/bin/perl -w | |
2 # | |
3 # A simple utility which will process each "*.txt" file inside a named | |
4 # directory. | |
5 # | |
6 # Any entries with a duplicate title will be alerted. | |
7 # | |
8 # Steve | |
9 # | |
10 | |
11 use strict; | |
12 use warnings; | |
13 | |
14 | |
15 # | |
16 # Get the directory and ensure it exists. | |
17 # | |
18 my $dir = shift; | |
19 die "Usage: $0 directory" unless ( defined($dir) ); | |
20 die "Not a directory : $dir" unless ( -d $dir ); | |
21 | |
22 | |
23 # | |
24 # The hash of arrays | |
25 # | |
26 my $titles; | |
27 | |
28 | |
29 # | |
30 # Find the files. | |
31 # | |
32 foreach my $file ( sort( glob( $dir . "/*" ) ) ) | |
33 { | |
34 | |
35 # | |
36 # Skip non-files | |
37 # | |
38 next if ( -d $file ); | |
39 next if ( $file =~ /~$/ ); | |
40 | |
41 # | |
42 # Title for this entry. | |
43 # | |
44 my $title = undef; | |
45 | |
46 # | |
47 # Read each file, and look for the title. | |
48 # | |
49 open( IN, "<", $file ) | |
50 or die "Failed to read $file - $!"; | |
51 foreach my $line (<IN>) | |
52 { | |
53 $title = $2 | |
54 if ( !defined($title) | |
55 && ( $line =~ /^(Subject|Title):(.*)/i ) ); | |
56 | |
57 } | |
58 close(IN); | |
59 | |
60 # | |
61 # Get current entries we might have found with | |
62 # this title; and add on the new one. | |
63 # | |
64 my $a = $titles->{ $title }; | |
65 push( @$a, $file ); | |
66 $titles->{ $title } = $a; | |
67 } | |
68 | |
69 | |
70 # | |
71 # Now look for dupes | |
72 # | |
73 my $fail = 0; | |
74 foreach my $title ( keys %$titles ) | |
75 { | |
76 my $a = $titles->{ $title }; | |
77 | |
78 if ( ( scalar @$a ) > 1 ) | |
79 { | |
80 print "Title: $title\n"; | |
81 foreach my $file (@$a) | |
82 { | |
83 print "\t" . $file . "\n"; | |
84 } | |
85 | |
86 $fail = 1; | |
87 } | |
88 } | |
89 | |
90 | |
91 exit($fail); |