Greetings, esteemed monks!

Here's what I've come up with:

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 - $startco +mpcopy; my $totalfiles = $newdirs + $newfiles + $changedfiles + $unchangedfile +s; 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 o +f 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 sti +ck \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"; } }

_________________________________________________________________________________

I like computer programming because it's like Legos for the mind.


In reply to Re: Best practices for file synchronization? (Mod time vs. contents compare) by OfficeLinebacker
in thread Best practices for file synchronization? (Mod time vs. contents compare) by OfficeLinebacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.