欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用perl清理电脑上重复文件实现代码(续)

程序员文章站 2022-04-29 19:15:20
复制代码 代码如下: #!/usr/bin/perl use file::dirwalk; use file::basename; use data::dumper; us...
复制代码 代码如下:

#!/usr/bin/perl
use file::dirwalk;
use file::basename;
use data::dumper;
use warnings;
use strict;
my $dw = new file::dirwalk;
my %files;
fileparse_set_fstype("mswin32");
$dw->onfile(
sub {
my ($file) = @_;
push @{$files{basename($file)}->{"paths"}},$file;
$files{basename($file)}->{"num"} += 1;
return file::dirwalk::success;
}
);
my $htrace;
open $htrace, '> trace.txt';
select $htrace;
$dw->walk('d:/old/perl');
my @newfiles;
while( my ($k, $v) = each %files)
{
if($v->{"num"} > 1)
{
#print $k."\n";
#print dumper($v);
push @newfiles,
{
"name"=>$k,
"paths"=>$v->{"paths"},
"num"=>$v->{"num"}
}
}
}
#print dumper(@newfiles);
@newfiles = sort {($a->{"num"}) <=> ($b->{"num"})} @newfiles;
print dumper(@newfiles);
close $htrace;