in reply to Strange problem trying to clean garbage from start of mailbox file

Now that martin has solved your basic problem, I'd just like to point out short your code could be:
my $path = "./mail"; my $bkup = "./backup"; open MAILDIR, $path; for my $mbox ( grep { -f "$path/$_" and -s _ } readdir MAILDIR ) { rename "$path/$mbox", "$bkup/$mbox"; system( "perl -ne 'print if /^From/..-1' $bkup/$mbox > $path/$mbox +" ); }
(That assumes that the backup directory is not on a distinct disk volume.)

update: as capoeiraolly points out below, that version backs up all mailbox files, not just the ones that need fixing. To avoid that, just add a few lines at the top of the for loop:

for my $mbox ( grep { -f "$path/$_" and -s _ } readdir MAILDIR ) { my $first = do { open M, "$path/$_"; <M> }; close M; next if ( $first =~ /^From / ); # ... do rename and system calls on bad files only rename "$path/$mbox", "$bkup/$mbox"; system( "perl -ne 'print if /^From/..-1' $bkup/$mbox > $path/$mbox +" ); }

Replies are listed 'Best First'.
Re^2: Strange problem trying to clean garbage from start of mailbox file
by capoeiraolly (Initiate) on Feb 03, 2006 at 04:19 UTC
    Cheers for that...

    Won't this code create a backup of every mailbox instead of just the corrupted ones?