I’ve got a hard disk with bad sectors and I’ve also got a shiney new one. So I’ve copied over the files from the old drive to the new. Alas, I’ve had to click “Skip” to get past the bad files and I have no idea which files didn’t make the transfer. A quick search online for a nice tool to compare the two drives has left be empty handed. I searched for “dirdiff” and everything, but nothing. And anyway I don’t want do know if the files are different, only what files are missing.
So I’ve had to resort to perl to write the following materpiece of coding wonder:
(Use it at your own risk)
use strict;
use warnings;
use Data::Dumper (‘Dumper’);
my $starting_point = $ARGV[0];
my $compare_folder = $ARGV[1];
if (! defined $starting_point or length($starting_point) == 0)
{
print "You must specify a starting foldern";
exit;
}
if (! -d $starting_point)
{
print "Your starting point must be a folder";
exit;
}
if (! defined $compare_folder or length($compare_folder) == 0)
{
print "You must specify a comparison foldern";
exit;
}
if (! -d $compare_folder)
{
print "Your comparison folder must be an actual folder";
exit;
}
my $folder_stack = [$starting_point];
process_all_folder($starting_point);
exit;
sub process_all_folder
{
my $full_path = join("/", @$folder_stack);
my $dir_handle;
opendir ($dir_handle, "$full_path")
or die $! . ": $full_pathn";
while (my $file = readdir($dir_handle))
{
# Um, need to escape the back slashes for the regex.
my $sp = $starting_point;
$sp =~ s/\/\\/g;
my $cp = $compare_folder;
my $comp_path = join("/", (@$folder_stack, $file));
$comp_path =~ s/^$sp/$cp/;
# Ignore the current and parent folders and also stuff in the
# RECYCLER
if ($file eq ‘.’ or $file eq ‘..’ or $file =~ /RECYCLER/)
{
next;
}
elsif (-d join("/", (@$folder_stack, $file)))
{
if (! -d $comp_path)
{
print "$comp_pathn";
}
# Recurse into folder is it is one…
push @$folder_stack, $file;
process_all_folder($file);
pop @$folder_stack;
}
else
{
if (! -f $comp_path)
{
print "$comp_pathn";
}
}
}
closedir $dir_handle;
}
exit;
I probably should have used a find command or something. But fancied doing a recursive thingy.
Home > About This Post
This entry was posted by Chris Andrews on Saturday, December 30th, 2006, at 3:40 am, and was filed in Technical.
Subscribe to the
RSS 2.0 feed for all comments to this post.
Post a Comment