This file is an attempt to find the unique images in a directory and write them to a new directory. Unfortuantly one of its problems is that the output file is a garbled thing that is not readable as a jpg. How can I fix that?
my $skip = 0; #must be set to zero intially
my @all_files = ();
my $pHandle = '';
my $vHandle = '';
my $image = '';
my $dir = 'D:\Earth\digits\\';
opendir (DIR, $dir) or die "cannot opendir $dir";
foreach my $file (readdir(DIR)) {
unless ($file eq '.' or $file eq '..') {
push(@all_files, $file);
}
}
closedir (DIR);
print "I made it past the dirtectory read\n";
#print "The files in the directory are:\n\n";
#foreach (@all_files) {
#print $_."\n";
#}
my %filehash = ();
foreach my $filename (@all_files) {
my $filesize = (stat('D:\Earth\digits\\'.$filename))[7];
if(defined $filehash{$filesize}) { $filehash{$filesize} += ", $filename"; }
else { $filehash{$filesize} = $filename; }
}
print "I made it past the first bit of Tumi's code\n";
print "The hash created with all the files in it is:\n\n";
foreach my $testkey (keys %filehash) {
print "$filehash{$testkey}\n";
}
foreach my $key (keys %filehash) {
unless ($filehash{$key} =~ /, /) {
open(PROD, "< D:\\Earth\\digits\\$filehash{$key}.jpg"
or die "Can't read the production file $!";
# write to a scalar
$image = '';
while(<PROD>)
{
$image .=$_
}
# Close the filehandle
close(PROD) or die "Can't close the production file.";
open(IMAGE, ">D:\\Earth\\unique\\$filehash{$key}.jpg"
|| die"$filehash{$key}: $!";
binmode IMAGE;
print IMAGE $image;
close IMAGE;
print "$filehash{$key} is unique and is saved away\n";
}
else { # do the unique read & compare check
my @files = split(/, /,$filehash{$key});
my @copyfiles = @files;
foreach my $fileA (@files) {
my $pPath = 'D:\Earth\digits\\'.$fileA.'.jpg';
open(PROD, "< $pPath"
or die "Can't read the production file";
# write to a scalar
$pHandle = '';
while(<PROD>)
{
$pHandle .=$_
}
# Close the filehandle
close(PROD) or die "Can't close the production file.";
foreach my $fileB (@copyfiles) {
my $vPath = 'D:\Earth\unique\\'.$fileB.'.jpg';
# Open the versioned file Read Only.
open(VERS, "< $vPath"
or $skip=1;
# write to a scalar
unless ($skip) {
$vHandle = '';
while(<VERS>)
{
$vHandle .=$_
}
# Close the filehandle
close(VERS) or die "Can't close the versioned file.";
# Compare the scalars
###### NOTE: this is a -- BINARY -- compare.
if($vHandle eq $pHandle)
{
#file already exists in unique directory and therefore is not unique
last;
}
else
{
# Continue -?
}
#if your still going through the loop then $vHandle never equaled $pHandle so the file is unique
open(IMAGE, ">D:\\Earth\\unique\\$fileA.jpg"
|| die"$pHandle: $!";
binmode IMAGE;
print IMAGE $pHandle;
close IMAGE;
print "$pHandle is unique and is saved away\n";
}
$skip = 0; #reset skip
print "I made it through the loop\n";
}
}
}
}
Celia
my $skip = 0; #must be set to zero intially
my @all_files = ();
my $pHandle = '';
my $vHandle = '';
my $image = '';
my $dir = 'D:\Earth\digits\\';
opendir (DIR, $dir) or die "cannot opendir $dir";
foreach my $file (readdir(DIR)) {
unless ($file eq '.' or $file eq '..') {
push(@all_files, $file);
}
}
closedir (DIR);
print "I made it past the dirtectory read\n";
#print "The files in the directory are:\n\n";
#foreach (@all_files) {
#print $_."\n";
#}
my %filehash = ();
foreach my $filename (@all_files) {
my $filesize = (stat('D:\Earth\digits\\'.$filename))[7];
if(defined $filehash{$filesize}) { $filehash{$filesize} += ", $filename"; }
else { $filehash{$filesize} = $filename; }
}
print "I made it past the first bit of Tumi's code\n";
print "The hash created with all the files in it is:\n\n";
foreach my $testkey (keys %filehash) {
print "$filehash{$testkey}\n";
}
foreach my $key (keys %filehash) {
unless ($filehash{$key} =~ /, /) {
open(PROD, "< D:\\Earth\\digits\\$filehash{$key}.jpg"
# write to a scalar
$image = '';
while(<PROD>)
{
$image .=$_
}
# Close the filehandle
close(PROD) or die "Can't close the production file.";
open(IMAGE, ">D:\\Earth\\unique\\$filehash{$key}.jpg"
binmode IMAGE;
print IMAGE $image;
close IMAGE;
print "$filehash{$key} is unique and is saved away\n";
}
else { # do the unique read & compare check
my @files = split(/, /,$filehash{$key});
my @copyfiles = @files;
foreach my $fileA (@files) {
my $pPath = 'D:\Earth\digits\\'.$fileA.'.jpg';
open(PROD, "< $pPath"
# write to a scalar
$pHandle = '';
while(<PROD>)
{
$pHandle .=$_
}
# Close the filehandle
close(PROD) or die "Can't close the production file.";
foreach my $fileB (@copyfiles) {
my $vPath = 'D:\Earth\unique\\'.$fileB.'.jpg';
# Open the versioned file Read Only.
open(VERS, "< $vPath"
# write to a scalar
unless ($skip) {
$vHandle = '';
while(<VERS>)
{
$vHandle .=$_
}
# Close the filehandle
close(VERS) or die "Can't close the versioned file.";
# Compare the scalars
###### NOTE: this is a -- BINARY -- compare.
if($vHandle eq $pHandle)
{
#file already exists in unique directory and therefore is not unique
last;
}
else
{
# Continue -?
}
#if your still going through the loop then $vHandle never equaled $pHandle so the file is unique
open(IMAGE, ">D:\\Earth\\unique\\$fileA.jpg"
binmode IMAGE;
print IMAGE $pHandle;
close IMAGE;
print "$pHandle is unique and is saved away\n";
}
$skip = 0; #reset skip
print "I made it through the loop\n";
}
}
}
}
Celia