dkaplowitz has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am new to programming and thus also new to Perl. I've been googling, using "Perl for System Administration", looking in various Perl CD Bookshelf volumes, but am not really clearly seeing the answers that are probably there looking me in the face. In English what I am researching how to do (in Win2k) is:
1) Recursively compare folderA (mapped) and folderB (local) and their subdirs.
2) Determine which files are out of sync (in this case meaning: different --newer-- versions exist, or one folder doesn't have the same files as the other.)
3) In the files where differences exist (in time stamps) copy the file(s) with the newer time stamp over to the second folder (the one with the older ---or missing --files, overwriting if necessary).
4) Copy the files that don't exist over, so that both directories have the same files
I've found something along the lines of:
my $file_date=localtime((stat("$file"))[9]);
for the time stamp info. I've found snippets of code here and there to come up with this:
#!/usr/bin/perl -w #!C:\Perl\bin\perl -w my $directory='/devl/bin'; my $size=0; my $file; my $file_date=localtime((stat("$file"))[9]); opendir(DIRHANDLE, $directory) or die "couldn't open $directory : $!\n +"; my@files = grep(!/^\.\.?$/,readdir(DIRHANDLE)); foreach $file (@files) { $size=(-s "$directory/$file"); print "$directory,$file,$size,$file_date"; print "\n"; } closedir(DIRHANDLE);
but that's about as far as I've gotten, which feels like a long way from the desired result.

What I'm still lost on finding out is how to recursively compare two separate directories, and also how the if different files, compare their dates, when date is newer (of if file doesn't exist in one directory), copy to directory missing file(s) and/or older versions.
Thanks for any clues.
  • Comment on recursively compare folders in windows, synchronize the two folders with the newest files
  • Select or Download Code

Replies are listed 'Best First'.
•Re: recursively compare folders in windows, synchronize the two folders with the newest files
by merlyn (Sage) on Mar 17, 2004 at 15:46 UTC
Re: recursively compare folders in windows, synchronize the two folders with the newest files
by matija (Priest) on Mar 17, 2004 at 16:14 UTC
    Why reinvent the wheel? (Unless you're doing this as a learning exercise, of course.)The people who wrote rsync have done the heavy lifing here, and the Perl interface is at File::Rsync.
Re: recursively compare folders in windows, synchronize the two folders with the newest files
by Abigail-II (Bishop) on Mar 17, 2004 at 16:33 UTC
    system rsync => $dir1, $dir2;
    But check the manual of rsync - you might want to use one or more of its options. And yes, rsync has been ported to Windows.

    Abigail

      Thanks for evenyone's replies. I got pulled from doing this task. When I get back to it I'm going to look into the rsync solution. I'll post my results back.