what this does: it classifies a file based on its modification date.

example: xfile1 is dated July 9, 2008.  If it doesn't exist, the
program creates a
directory structure 2008/July/09 and places xfile1 there.
Then it creates a log with the steps done.

So...in the system the result is 2008/July/09/xfile1
The log entry looks like "xfile1 => 2008/July/09"

problem: When I send an INT signal (Control+C) from the keyboard for
example,
it exists nicely, it finishes whatever file it needed to finish, and
it classifies the file
properly.  However, it misses some entries on the log file.  Out of
4,000 files if you INT the program twice or three times it won't write
about 2 or 3 entries on the log.

How can I not lose an entry on the log file? if you have other
suggestions to make this code
more efficient please let me know. Thanks in advance.

Code:

#!/usr/bin/perl
use warnings;
use strict;
use File::Copy;
use File::Path;
use File::Find;
use Time::localtime;
no warnings 'File::Find';


my @target_directories = ("/full/path/to/test1", "/full/path/to/
test2");
my $files_moved = "test.log";
my $files_moved_fh;
my $source_dir_handler;
my $signal;
my $list;
my $condition = 1;


$| = 1; #flush buffers

my @months =
("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
my @all_signals = qw (STOP KILL ABRT QUIT TERM INT TSTP HUP);

sub signal_handler {
    $signal = shift;
    $condition = 0;
}


find( { wanted => \&process, no_chdir => 0 }, @target_directories );

sub process {

    foreach $list(@all_signals){
        $SIG{$list} = \&signal_handler;
    }

    while ( $condition == 1 ) {
        foreach my $xdir (@target_directories){
                        opendir ($source_dir_handler, $xdir) or die $!;
                        chdir $xdir;

                        while (defined (my $xfile = readdir 
($source_dir_handler))){

                                        #skip . and .. files and directories
                                        next if ($xfile =~ m/^\./);  #skip dot 
files . and ..
                                next if -d $xfile;

                                #extract year, month, day when $_ was last 
modified
                                        my $calendar = 
localtime((stat($xfile))[9]);
                                        my $dd = $calendar->mday;
                                        my $mm = $calendar->mon;
                                        my $yyyy = $calendar->year+1900;
                                        my $dir = $yyyy . '/' . $months[$mm] . 
'/' . $dd;  #eg 2008/July/
9

                                if (! -d $dir and ! -e $dir ){
                                                mkpath([$dir], 0, 0750) or die 
$!;
                                        move ($xfile, $dir) or die $!;
                                        open ($files_moved_fh, ">>", 
$files_moved) or die $!;
                                        print $files_moved_fh "$xfile => 
$dir\n";
                                        close $files_moved_fh;
                                }
                                else {
                                        move ($xfile, $dir) or die $!;
                                        open ($files_moved_fh, ">>", 
$files_moved) or die $!;
                                        print $files_moved_fh "$xfile => 
$dir\n";
                                        close $files_moved_fh;
                                }

                        }

                }
                 $condition = 0;
      }
 }


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to