遍历当前目录的所有文件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!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";
	}
	}