如何使用file:find查找重复的文件名? [英] how to find duplicate file name using file:find?
问题描述
我正在尝试编写一个程序,以使用perl在多个驱动器中查找重复的文件名.这是我的脚本,但是它提供了错误的信息.
I am trying to write a program to find duplicate file names in multiple drive using perl. This is my script, but it gives wrong info.
#!/usr/bin/perl
use File::Find;
@filename;
@path;
$count;
open my $out, '>', 'output.txt' or die $!;
my $file = "E:";
find( \&edit, "$file" );
sub edit() {
$count++;
push( @filename, $_ );
push( @path, $File::Find::name );
}
print "Processing.";
for ( my $i = 0 ; $i < $count ; $i++ ) {
for ( my $j = $i + 1 ; $j < $count ; $j++ ) {
if ( $filename[$i] =~ /\Q$filename[$j]\E/ ) {
print $out("$filename[$i] = $filename[$j]\n");
print ".";
}
}
}
推荐答案
您应始终包含 use strict;
和 use warnings;
在每个perl脚本中.但是,您很幸运,但这并没有导致任何错误.
You should always include use strict;
and use warnings;
in EVERY perl script. However, you lucked and that didn't lead to any errors.
实际上,除了使用正则表达式测试何时应该使用 eq
,您的脚本看起来可以正常工作.但是,作为样式更改,我会将所有路径保存在数组的哈希中,以便更轻松地找到匹配的文件.尤其是目前,您的方法无法将3个或3个以上的组一起列出.
In fact, other than using a regex to test when you should've used eq
, your script looks functional. As a style change though, I would've saved all paths in a hash of arrays to more easily find matching files. Especially as currently your method wouldn't list groups of 3 or more together.
use strict;
use warnings;
use autodie;
use File::Find;
my %files;
open my $out, '>', 'output.txt';
my $file = "E:";
find( \&edit, "$file" );
sub edit() {
push @{$files{$_}}, $File::Find::name;
}
while (my ($file, $paths) = each %files) {
next if @$paths == 1;
print "$file @$paths\n";
}
这篇关于如何使用file:find查找重复的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!