use strict; use File::Copy; use File::Find; use File::Compare; use Time::HiRes qw(time); my $source = "G:\\contingency\\"; my $maindest = "D:\\"; ( -r $source ) || die "Error: $source not readable\n"; ( -e $maindest ) || die "Error: $maindest (memory stick) does not exist--is your memory stick attached?\n"; print "Beginning scan of source directory for updated files...\n\n"; # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; my $newdirs = 0; my $newfiles = 0; my $changedfiles = 0; my $unchangedfiles = 0; my $orphans = 0; my $startcompcopy = time; my $status1 = find( \&wantedtime, $source ); my $endcompcopy = time; printf "Synchronization took %f seconds.\n\n", $endcompcopy - $startcompcopy; my $totalfiles = $newdirs + $newfiles + $changedfiles + $unchangedfiles; print "Summary report: \n"; print "$totalfiles entries scanned.\n"; print "$newdirs new directories found and created.\n"; print "$newfiles new files found and copied.\n"; print "$changedfiles updated files copied.\n"; print "$unchangedfiles unchanged files ignored.\n\n"; #..and then take care of orphans print "searching for orphans:\n"; my $dest1 = $source; $dest1 =~ s/G:/D:/; my @filesizes; #use finddepth here cos we're deleting my $status1 = finddepth( \&wantedorphans, $dest1 ); print "$orphans orphans deleted from memory stick.\n\n"; print "This program will now pause for 10 seconds, then exit.\n"; sleep 10; sub wantedtime { my $dest = $name; $dest =~ s/G:/D:/; if ( not -e $dest ) { if ( -d $name ) { print "Making new directory $dest\n"; mkdir $dest; ++$newdirs; } else { print "Copying previously nonexistent file $dest\n"; my $cstatus = copy( $name, $dest ); #copy returns 1 on success ++$newfiles; ($cstatus) || warn "WARNING: Copy of $name failed with status $cstatus: $!\n"; } } else { #Not doing this for directories as empty dirs are taken care of above #and copying an entire directory won't work with copy() if ( -d $name ) { print "scanning $name...\n"; } else { my $smtime = ( stat($name) )[9]; my $dmtime = ( stat($dest) )[9]; if ( ( $smtime > $dmtime ) ) { print "Updated file found!\n Copying $name\n to memory stick \n"; my $cstatus = copy( $name, $dest ); #copy returns 1 on success ($cstatus) || warn "WARNING: Copy of $name failed with status $cstatus: $!\n"; ++$changedfiles; } else { ++$unchangedfiles; } } } } sub wantedorphans { my $status = 1; $source = $name; $source =~ s/D:/G:/; if ( not -e $source ) { ++$orphans; print "$name is an orphan; deleting...\n"; if ( -d $name ) { $status = rmdir $name; } else { $status = unlink $name; } #rmidr returns true on success; unlink returns number of files deleted (should be 1) ($status) || warn "WARNING: Status of removal of $name is $status : $!\n"; } }