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

幾個知識點:

use Cwd  引用當前目錄

cwd:當前目錄

shift:獲取位置參數;

chdir 改變目錄位置

opendir 打開目錄

closedir 關閉目錄

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('.');


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章