On Jul 10, 8:36 am, [EMAIL PROTECTED] (John W. Krahn) wrote: > icarus wrote: > > 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. > > It sounds like you are "Suffering from > Buffering"http://perl.plover.com/FAQs/Buffering.html > > What you need to do is open the log file once at the beginning of the > program and then apply autoflush to the log filehandle. > > > 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. > > Perhaps something like this (UNTESTED): > > #!/usr/bin/perl > use warnings; > use strict; > use File::Copy 'move'; > use File::Path 'mkpath'; > use POSIX 'strftime'; > > my @target_directories = ( > '/full/path/to/test1', > '/full/path/to/test2', > ); > my $files_moved = 'test.log'; > my $condition = 0; > > open my $LOG, '>>', $files_moved or die "Cannot open '$files_moved' $!"; > select $LOG; > $| = 1; #flush buffers on $files_moved_fh > > my @all_signals = qw( STOP KILL ABRT QUIT TERM INT TSTP HUP ); > sub signal_handler { > my $signal = shift; > $condition = 1; > } > for my $list ( @all_signals ) { > $SIG{ $list } = \&signal_handler; > } > > for my $xdir ( @target_directories ) { > chdir $xdir or die "Cannot chdir '$xdir' $!"; > opendir my $source_dir, '.' or die "Cannot open '$xdir' $!"; > > while ( my $xfile = readdir $source_dir ) { > > # skip directories including . and .. > next if -d $xfile; > > #extract year, month, day when $xfile was last modified > # eg 2008/July/9 > my $dir = strftime '%Y/%b/%-d', localtime( ( lstat $xfile )[9] ) > > if ( ! -e $dir ) { > mkpath [ $dir ], 0, 0750 or die "Cannot mkpath '$dir' $!"; > } > move $xfile, $dir or die "Cannot move '$xfile' $!"; > # print to the autoflushed log file now > print "$xfile => $dir\n"; > exit if $condition; > } > } > > __END__ > > John > -- > Perl isn't a toolbox, but a small machine shop where you > can special-order certain sorts of tools at low cost and > in short order. -- Larry Wall
John, you da man!! That tip you gave me was right on the money. Yes, I was suffering from buffering. I really appreciate your help. Thanks. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/