Perl 編程練習-文件遍歷操做

幾個知識點:ide

use Cwd  引用當前目錄it

cwd:當前目錄class

shift:獲取位置參數;sed

chdir 改變目錄位置foreach

opendir 打開目錄file

closedir 關閉目錄perl

foreach 。。。next引用

#!perl -w
use strict;
use Cwd;  //
sub scanDirectory {
    my $workdir=shift;
    my $startdir=cwd;
    chdir $workdir or die "Unable to enter the $workdir dir!\n";
    opendir my $DIR,'.' or die "can't open the dir\n";
    my @names = readdir $DIR or die "can't readdir\n";
    closedir $DIR;
    foreach my $name(@names){
        next if($name eq '.');
        next if($name eq '..');
        if(-d $name){
            scanDirectory($name);
            next;
        }
        if($name eq 'core.txt'){
            print "find a core file! Path:". cwd ."\n";
            next;
    }
    }
    chdir $startdir or die "Unable to enter the $startdir dir!\n";
}
&scanDirectory('.');
相關文章
相關標籤/搜索