Help with script to move email files based on date field in header

M

mw

I have several approx 22,000 emails from various mailing lists (Maildir
format) in a directory that I wish to sort and move to another directory
based on the date field in the header.

ie: date in email header
Date: 26 Apr 2003 05:53:41 -0000

ie:
Date: Sat, 28 May 2003 21:44:27 -0400

Basically I would a script that would move all emails with the word Apr
in the email date header to a directory called April. All emails with
the word May in the email date header to be moved to the May directory
and so on...

I have tried doing it with MUA filtering, however it is painfully
slow....and my knowledge of grep and bash/perl scripting is at best
painfully weak. I have googled/played with this for hours, however I
still cannot get a functional script of any sort to work.

I would appreciate any advice or pointers.

Regards,
Mark
 
R

Roy Johnson

mw said:
ie:
Date: Sat, 28 May 2003 21:44:27 -0400

Basically I would a script that would move all emails with the word Apr
in the email date header to a directory called April. All emails with
the word May in the email date header to be moved to the May directory
and so on...

This ought to do what you want, except for simplicity, I named the
directories with the three-letter month abbreviations expected in the
files.

my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my $pat = join('|', @mons);

for (@mons) {
mkdir $_ or warn "Could not make dir $_: $!\n";
}
opendir DIR, '.' or die "Could not read .: $!\n";
while (my $f = readdir DIR) {
next unless -f $f;
open IN, $f or warn "Could not open $f: $!\n";
while (<IN>) {
if (/^Date:/) {
if (/($pat)/o) {
rename $f, $1/$f or warn "Could not move $f to $1\n";
}
else {
warn "No month found in $f\n";
}
last;
}
}
}
 
M

Mark

Basically I would a script that would move all emails with the word Apr
in the email date header to a directory called April. All emails with
the word May in the email date header to be moved to the May directory
and so on...

This ought to do what you want, except for simplicity, I named the
directories with the three-letter month abbreviations expected in the
files.

my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my $pat = join('|', @mons);

for (@mons) {
mkdir $_ or warn "Could not make dir $_: $!\n";
}
opendir DIR, '.' or die "Could not read .: $!\n";
while (my $f = readdir DIR) {
next unless -f $f;
open IN, $f or warn "Could not open $f: $!\n";
while (<IN>) {
if (/^Date:/) {
if (/($pat)/o) {
rename $f, $1/$f or warn "Could not move $f to $1\n";
}
else {
warn "No month found in $f\n";
}
last;
}
}
}

There is a problem with this script. I run it and get an error message:
Illegal division by zero at datescript.pl (nameof the script)line 14,
<IN> line 788.
It then deletes ALL the files in the current dicretory and leaves a file
with the name of 0 (zero). This is not a good thing.....I am glad I
tested it on a test directory.
Can anyone please tell me what needs to be fixed with this script
 
J

Jürgen Exner

Mark wrote:
[...]
rename $f, $1/$f or warn "Could not move $f to $1\n";
[...]

There is a problem with this script. I run it and get an error
message: Illegal division by zero at datescript.pl (nameof the
script)line 14, <IN> line 788.

Well, yeah, you are dividing $1 by $f.
Guessing from the rest of the code $f is probalby some file name and unless
this file name happens to start with digits its numerical value would be 0.
So sure, you are getting a division by zero error.

Maybe you meant
rename $f, "$1/$f" or warn "Could not move $f to $1\n";
It then deletes ALL the files in the current dicretory and leaves a
file with the name of 0 (zero).

Well, not exactly. One by one it renames all files to "0", thereby removing
one by one whatever file it had renamed in the previous iteration.

jue
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top