遍历当前目录的所有文件

2015-12-14 0 252
遍历当前目录的所有文件
#!perl
#
#Author:caryl
#
#

use strict;
use Cwd;
use File::Spec;
use File::Basename;

open STDOUT, ">filelist.txt" or die "can't open file filelist.txt:$!";
my $cwd = getcwd;
my @files = <*>;
my $file;
foreach $file(@files)
{
	my $path = File::Spec->catfile( $cwd, $file );
	&filelist($path);
}

sub filelist
{
	my $path = shift @_;
	my $file_name = basename $path;
	if(-d $file_name)#如果是文件,进入遍历
	{
		print "$path\n";
		chdir $path or die "can't chdir $path:$!";
		my $cwd = getcwd;
		my @files = <*>;
		my $file;
		my $count = 0;
		foreach $file(@files)
		{
			$count++;
			my $path = File::Spec->catfile( $cwd, $file );
			&filelist($path);
		}
		if ($count eq @files)#如果当前目录遍历完成,回到父目录
		{
			my $dir_name = dirname $path; 
			chdir "$dir_name\.." or die "can't chdir $dir_name\..:$!";
		}
	}
	else
	{
		print "$path\n";
	}
	}

遇见资源网 perl 遍历当前目录的所有文件 http://www.ox520.com/13076.html

上一篇:

已经没有上一篇了!

下一篇:

已经没有下一篇了!

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务