I’m trying to write a perl script than can open a dir and delete duplicate files by comparing part of their names and modified date.
I’ve based part of my code using bubblesort algorithm.
The problem I’m having though is the file names are like this: test12345.log, test34333.log, mytest11111.log, mytest22222.log, etc. I need to remove the numerical values at the end of the file name and then just compare the remaining names and modified date. test and test are duplicates but which one has the older modified date so I can delete it?
Here’s what I’ve written so far:
#!D:\Perl\bin\perl
opendir (DIR,".");
@array = grep(/.log/,readdir(DIR));
closedir (DIR);
foreach(@array){
s/[0-9]{1}.log$//g;
}
dlete(\@array);
sub dlete {
my $array = shift;
my $not_complete = 1;
my $index;
my $len = ((scalar @$array) - 2);
while ($not_complete) {
$not_complete = 0;
foreach $index (0 .. $len) {
if (@$array[$index] eq @$array[$index+1] && -M @$array[$index] > -M @$array[$index + 1]) {
unlink "@$array[$index]";
$not_complete = 1;
}
}
}
}
The question I’m asking is above the coding. I’m trying to delete duplicate files from a dir by comparing their names & modified date. The problem I’m having though is the file names are like this: test12345.log, test34333.log mytest11111.log, mytest22222.log etc. i need to remove the numerical values at the end of the file name and then just compare the remaining names and modified date. eg test and test are duplicates but which one has the older modifed date so i can delete it
Here is my suggestion for my understanding of your problem.
please check Win32::UTCFileTime for correct times when using stat.
*