Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

[Win] Compare files between two directories (and subdir)

by jeepj (Scribe)
on Apr 08, 2008 at 14:36 UTC ( #678994=snippet: print w/ replies, xml ) Need Help??

Description:

I have written this script because I am maintaining a little set of Perl libraries and tools at work, and I'm modifying the installed files on my PC. To avoid erasing it with old versions from CVS, I have the CVS repository elsewhere, and this little script is giving me which file is different from the repository in my current installation

I know the code is not error proof, but it does the work, and it's all I need

use strict;
use File::Path;
use File::Find;

my $repodir='D:\Documents\Works\Perl\Perl tools\repository\amaPerl';
my $instdir='D:\AmaPerl';


my %options;
$options{'wanted'}=\&compare_files;
$options{'no_chdir'}=1;
find(\%options,  ($repodir));

exit(0);

sub compare_files
{
    return if($_ eq '.' || $_ eq '..' || $File::Find::dir =~ /CVS/);
    if(-f $File::Find::name) {
        my $file=substr($File::Find::name,length($repodir)+1);
        my $other_file=$instdir."\\".$file;
        if(! -f $other_file) {
            print "$file not found installed...\n";
        } else {
            system("diff -q \"$File::Find::name\" \"$other_file\" > NU
+L");
            if($?) {
                print "$file is different\n";
            }
        }
    }
}
Comment on [Win] Compare files between two directories (and subdir)
Download Code
Re: [Win] Compare files between two directories (and subdir)
by Arunbear (Parson) on Apr 09, 2008 at 09:11 UTC
    system("diff -q \"$File::Find::name\" \"$other_file\" > NU +L"); if($?) { print "$file is different\n"; }
    could be simplified to
    if(qx/diff -q "$File::Find::name" "$other_file"/) { print "$file is different\n"; }
    Update: Added quotes around file name variables to handle file names with spaces in them (thanks ikegami).
Re: [Win] Compare files between two directories (and subdir)
by Muggins (Pilgrim) on Apr 11, 2008 at 12:46 UTC

    Hmm I had a very little thing that uses Text::Diff and File::Dircmp (which I think does what you want) to do a good compare of two directories. You can do a compare, a Text::Diff::diff, or a UNIX-ilke diff on the files from it.

    Is this the sort of thing you mean? Will find it and put in my scratchpad...(wanders off)

      Exactly ! The only thing is that only files from the cvs dir are of interest, any additional file in my other directory are just tests and temp scripts. If you find it, I'm interested. I write this thing quickly, just to do the job, but I would be glad to see another solution.
Re: [Win] Compare files between two directories (and subdir)
by Anonymous Monk on Apr 11, 2008 at 15:38 UTC
    Is there any reason 'cvs diff' doesn't work?
      No, it's not what I wanted. I have a working directory when I test new things and modifications, and I have my CVS dir, where I prepare what is to be committed to CVS. I just have this script to check that I didn't forget any modifications for CVS check-in.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://678994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-07-12 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls