专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>perl>

perl遍历文件夹的方法-使用File::Find

百度收藏 QQ搜藏

http://www.chinaunix.net 作者:路小佳
本文介绍遍历文件夹方法:使用File::Find,先看代码:
use File::Find

#!/usr/bin/perl -W
# File: find.pl
# License: GPL-2

use strict;
use warnings;
use File::Find;

my ($size, $dircnt, $filecnt) = (0, 0, 0);

sub process {
    my $file = $File::Find::name;
    #print $file, "\n";
    if (-d $file) {
        $dircnt++;
    }
    else {
        $filecnt++;
        $size += -s $file;
    }
}

find(\&process, '.');
print "$filecnt files, $dircnt directory. $size bytes.\n";

下面是对我的硬盘/dev/hda6的测试结果:

26881 files, 1603 directory. 9052479946 bytes.

real    0m9.140s
user    0m3.124s
sys     0m5.811s

上一篇:perl中的->符号的主要用法解释 下一篇:perl遍历文件夹的方法-lsr递归遍历

power by soyo123 2007-2008