Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Ver. 1.2: Compare Directories and List Changed or Missing Files

by sierrathedog04 (Hermit)
on Sep 19, 2001 at 21:03 UTC ( #113408=note: print w/replies, xml ) Need Help??


in reply to Compare Directories and List Changed or Missing Files

# sierrathedog04, jonathansamuel@yahoo.com, August 2001 + # First parameter is the target or VOB directory. + # Optional second parameter is the source or current directory. + + # Ver. 1.1, Sept. 13, 2001. Now uses File::Compare instead of diff; + # Modified printouts so only files are listed. (Not directories). + # Ver. 1.2 Sept. 19, 2001. Accepts optional parameters 'modified' and +'created' followed by directory names. If these parameters are used then the program wil +l copy all modified files to the modified directory and all changed files to the changed +directory. + use strict; + require File::Compare; + + use Getopt::Long; + my $modifiedDir; + my $createdDir; + GetOptions("modified:s" => \$modifiedDir, "created:s", => \$createdDir +); $| = 1; + + print "\n"; + my $targetDir = shift || die "Please pass the name of a target directo +ry enclosed in quotes. \n"; + chomp $targetDir; + $targetDir =~ s#/$##; # Removes any trailing forward slash from the di +rectory. unless (-d $targetDir) { + die "$targetDir is not a valid target directory.\nPlease pass the +name of a valid target directory enclosed in quotes. \n"; } my $sourceDir = shift || "."; unless (-d $sourceDir) { die "$sourceDir is not a valid source directory.\nPlease pass the +name of a valid source directory enclosed in quotes. \n"; } if (defined $modifiedDir){ unless (-d $modifiedDir) { die "$modifiedDir is not a valid modified directory. \ +nPlease use the --modified option to pass the name of a valid directo +ry \nto which you wish to copy all modified files."; } } if (defined $createdDir){ unless (-d $createdDir) { die "$createdDir is not a valid created directory. \nP +lease use the --created option to pass the name of a valid directory +\nto which you wish to copy all created files."; } } opendir THISDIR, $sourceDir; my @allFiles = grep { $_ ne '.' and $_ ne '..' && -f "$sourceDir/$_"} + readdir THISDIR; closedir THISDIR; foreach (@allFiles) { if (File::Compare::compare("$sourceDir/$_", "$targetDir/$_") = += 1) { print "$_ in source directory $sourceDir differs from + that in target directory $targetDir\n\n"; print `cp $sourceDir/$_ $modifiedDir/$_` if defined $ +modifiedDir; } elsif (File::Compare::compare("$sourceDir/$_", "$targetDir/$ +_") < 0) { print "$_ found in source directory $sourceDir but not + in target directory $targetDir\n\n"; print `cp $sourceDir/$_ $createdDir/$_` if defined $m +odifiedDir; } } print "\n...Finished checking source.\n\n\n"; opendir targetDIR, $targetDir; my @alltargetFiles = grep { $_ ne '.' and $_ ne '..' && -f $_} readd +ir targetDIR; foreach (@alltargetFiles) { print "$_ found in target directory $targetDir but not in sour +ce directory $sourceDir\n\n" if File::Compare::compare("$sourceDir/$_ +", "$targetDir/$_") < 0; }

Replies are listed 'Best First'.
Re: Ver. 1.2: Compare Directories and List Changed or Missing Files
by Anonymous Monk on Mar 05, 2012 at 20:53 UTC
    my @alltargetFiles = grep { $_ ne '.' and $_ ne '..' && -f $_} readdir targetDIR;
    ought to be
    my @alltargetFiles = grep { $_ ne '.' and $_ ne '..' && -f "$targetDir/$_"} readdir targetDIR;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://113408]
help
Chatterbox?
[ambrus]: Now it has five people instead of three, some sort of call number ticket system where people wait a lot for their food to get ready (it's the same kinds of grilled meat and fish on the same equipment, it won't actually fry slower),
[ambrus]: it's slow as hell, and the food costs significantly more.
[ambrus]: We no longer eat there.
LanX Everybody quotes it, some people read it, and a few people go by it
[ambrus]: LanX: why would I read it? isn't it a book for managers? I don't want to be a manager.
[ambrus]: I don't quote it either.
[erix]: it's really a software book, albeit about experiences from the 60s-80s. People haven't changed all that much though.
[erix]: a software project book, is probably more exact
[ambrus]: Yes, but I'm not managing software teams either.
[ambrus]: I'm not only not a restaurant manager, but also not a software manager.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2017-09-22 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (264 votes). Check out past polls.

    Notices?