这个其实挺简单的,给你写几句比较通俗的希望对你有所帮助啊;
my @pathlist='find /home/a/ -name "*.sp"';
#这是执行linux命令行中的命令,把/home/a/ 路径下所有的.sp文件都检索出来,放到@pathlist数组里。
foreach my $fsp (@pathlist) {
#想要的文件出理。
open(OUT,“〈",$fsp);
}
使用File::Find模块吧,很简单方便。
#!/usr/bin/perl -w
use strict;
use File::Find;
sub wanted{
if(/\.sp$/){
print $File::Find::name;#输出路径跟文件名
#你要进行的操作
}
}
find \&wanted,'/home/a';
sub gDirTree {
my $dir=shift;
my @gFile=();
if (!-d "$dir" && -e "$dir") {
push(@gFile,$dir);
return @gFile;
}
return if (!-d "$dir" || $dir =~ /\.$/);
my @files=grep {!(/^\./)} glob("$dir/*");
for my $nextname(@files ) {
if(-d "$nextname") {
push(@gFile,&gDirTree($nextname));
} else {
push(@gFile,$nextname);
}
}
return @gFile;
}
my @Files=&gDirTree("目录名");
以上子程序获取目录下所有的文件(含子目录),你可以针对性的处理
sub process
{
my $file =shift;
}
sub finddir{
my $dir=shift;
my $h;
opendir($h,$dir)||return;
my @cont=readdir($h);
for my $f($cont)
{
my $path=$dir .'\'.$f;
if(-d $path)
{
if($f ne '.' && $f ne '..'){finddir($path);}
}
else if ($f =~/\.sp$/)
{
process($path);
}
}
}