遍历目录下所有的文件

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

#!perl
#
#作者:赵灿星
#本perl脚本用于遍历目录下所有文件,使用时请将本脚本拷贝到需要遍历的目录下,双击即可
#
#

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