Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polling a directory and finding relevant files

Status
Not open for further replies.

mow14

Technical User
May 12, 2008
4
IE
Hi,

I'm new here so hopefully i'm posting this in the right forum! I'm pretty new to perl also so this question is probably straight forward for most of ye!

Basically i'm writing a script to poll a directory and get a list of the files, this part is ok! I have been reading the files into an array (not sure if this is the best way to do this for what i am trying to achieve). Once i have the list i then need to compare all the files and search for file pairs that have the same name but have different suffix, for example

file.txt
file.tmp

Does anyone know a nifty way to compare array elements to achieve this? I have been attempting to use sort but am having little joy!

Cheers!

 
Module File:Basename contains a routine that does what you want. Here I have loaded the file names into an array, and used a hash to check for uniqueness of the file name part. But you can do whatever else you need inside the if-else blocks
Code:
use strict;
use warnings;
use File::Basename;

my @files;
my %uniq;

@files = <DATA>;
chomp @files;

foreach (@files) {
  my ($file, undef, $suff) = fileparse($_, '.\w+');
  if (exists($uniq{$file})) {
    print $file . $suff . ": this file already exists with a \"" . $uniq{$file} . "\" suffix\n";
  }
  else {
    $uniq{$file} = $suff
  }
}  

__DATA__
file1.txt
file2.txt
file2.gif

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Great thanks, that looks to have worked but now i am stumbling over how to add multiple values to each key in a hash. I want each different suffix to be one of multiple values for the filename key, i have been attempting to do it as below

foreach (@finallist){
my ($file, undef, $suff) = fileparse($_, '.\w+');
if ($suff eq ".cdr"){
if (exists($uniq{$file}{$suff})){
print $file . $suff . ": this file already exists with a \"" . $uniq{$file} . "\" suffix\n";
}
else {
$uniq{"$file"} = ({"suffix1"=>"$suff"});
print "1: $uniq{$file}{suffix1}\n";
}
}
elsif ($suff eq ".filt"){
if (exists($uniq{$file}{$suff})){
print $file . $suff . ": this file already exists with a \"" . $uniq{$file} . "\" suffix\n";
}
else {
$uniq{"$file"} = ({"suffix2"=>"$suff"});
print "2: $uniq{$file}{suffix2}\n";
}
}
else {
print "error in locating files1\n";
exit;
}

}

and then trying to check if both suffix exist for a particular key with the following

foreach my $key (sort keys %uniq){
#print "$key: @{$uniq{$key}}\n";
if ((exists($uniq{$key}{suffix1}) && ($uniq{$key}{suffix2}))){
push(@list, $uniq{$key}{suffix1});
push(@list, $uniq{$key}{suffix2});
}
}


Needless to say it is not working, the values appear to being populated in the hash but when i try to read them in the second foreach i am having problems.

Thanks
 
If you want to check for duplicates, use a hash of hashes, as it will do the work for you. In the example below, the file names and suffixes are the keys to the hashes, and the value is the number of times that file was found.
Code:
use strict;
use warnings;
use File::Basename;
use Data::Dumper;

my @finallist;
my %uniq;

@finallist = <DATA>;
chomp @finallist;

foreach (@finallist) {
  my ($file, undef, $suff) = fileparse($_, '.\w+');
  $uniq{$file}->{$suff}++;
}  

print Dumper(%uniq);

__DATA__
file1.txt
file2.txt
file2.gif
file2.txt

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top