perl --Recursion find folder and file

1 c:\Perl\bin\perl.exe  findfile.pl
#!/usr/perl/bin/perl
##C:\\MIG-WORK\\Research\\SPOOL\\main
open(OFH, ">C:\\MIG-WORK\\Research\\SPOOL\\result.txt") || die("cna't open the file\n");
open(SOFH, ">C:\\MIG-WORK\\Research\\SPOOL\\spoolresult.txt") || die("cna't open the file\n");
my $sqlcount =0;
my $sqlspool=0;
$PATH="C:\\MIG-WORK\\Research\\SPOOL\\main";
&SeeDir($PATH);
print OFH "total sql :".$sqlcount."\n";
print OFH "spool sql :".$sqlspool."\n";
print "total sql :".$sqlcount."\n";
print "spool sql :".$sqlspool."\n";
print SOFH "total sql :".$sqlcount."\n";
print SOFH "spool sql :".$sqlspool."\n";
close(OFH);
close(SOFH);

sub SeeDir
 {
 my ($path,$DIR)=@_;
 if(chdir($path))
 {
 	opendir($DIR,$path)|| die "can't open directory directory $path";
 	my $line=readdir($DIR);
 	while ($line)
 	{
		if($line ne '.' && $line ne '..')
		{
			my $subpath =  $path."\\".$line;
			if(chdir($subpath))
			{
			    print " **** $line ****\n\n";
				SeeDir($subpath);
			}
			else
			{
				if($line=~/\.sql$/i)
				{
					$sqlcount++;
					#print " **** $line ****\n";
					print OFH " **** $line ****\n";
					my $sflag = &findspool($subpath);
					##print $sflag."sflag **\n";
					if($sflag > 0)
					{
						$sqlspool++;
						#print SOFH "-----$line------\n";
						#print SOFH "-----------------\n";
					}
					#print " **** $line ****\n\n";
					#print OFH " **** $line ****\n\n";
				}
			}
		}
		$line=readdir($DIR);
   }
 closedir($DIR);
 }
 } 
 
 sub findspool
 {
	my $flag = 0;
	my ($file, $FHD) = @_;
	open(FHD, $file) || die("cna't open the $file\n");
	while($fline = <FHD>)
	{
		chomp($fline);
		$fline=~s/^\s+|\s+$//g;
		if($fline=~/^spool/i)
		{
			print OFH "$fline\n";
			print SOFH "$fline\n";
			#print "$fline \n";
			$flag =1;
		}
	}
	if($flag >0)
	{
		#print SOFH "-----------------\n";
	}
	close(FHD);
	return $flag;
 }
 
 2  c:\Perl\bin\perl.exe  findfile.pl
 #!/usr/perl/bin/perl
$PATH="C:\\MIG-WORK\\Research\\SPOOL\\main\\";
push (my @dirs, $PATH);

while(@dirs)
{
	my $rcdir = shift @dirs;
	opendir(DIR,$rcdir) or die "can't open dir $rcdir \n";
	while(my $name = readdir(DIR))
	{
			chomp($name);
			if($name!~/^\./)
			{
				my $filename = $rcdir.$name;
				if(-f $filename)
				{
					#print $filename." :file\n";
				}
				else
				{
					push (@dirs, $filename."\\");
					print $filename." :folder \n";
				}
			}
	}
}
http://bbs.csdn.net/topics/20470253
http://www.mail-archive.com/perlchina@googlegroups.com/msg02611.html

  

转载于:https://www.cnblogs.com/cquccy/archive/2013/04/11/3014341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值