Perl 脚本在给定的文件夹目录中递归查找文件

最近在工作中使用到了Perl 脚本,需要在给定目录下对各类编译过的文件进行 Valgrind 安全检查。 谢了一个脚本自动将检查结果写入到文本文件当中。
程序名为 process_files:

转载请注明出处: http://blog.csdn.net/elfprincexu


原理: 从根目录(给定参数)开始查找, 如果找到的文件仍然为子文件夹,递归调用。

        如果找到的是文件,则判断是否是我们感兴趣的文件。

     查找过程中,排除linux下 .  .. 文件夹

             最后将结果输出到文本文件,便于查看和纠错。


主要函数: process_files


#!/usr/bin/perl
use warnings;
use strict;

use IO::Handle;
use File::stat;
use Term::ANSIColor;


STDOUT->autoflush(1);
my $binStr = "bin.x86_64";
my $dir = '/rapid/rapid_workspaces/jxu/jxu_RTC_New/Fusion/SW';
print ("fusion dir is :$dir\n");

my $wantedfiles = 0;
my $processedfs =0;
my $target = 'valgrind.log';
my $processedfiles = 'valgrindProcessFiles.log';


system ("rm -f $processedfiles");  #remove it
system ("touch $processedfiles");  #create it
print ("processedfiles file is :$processedfiles\n");


system ("rm -f $target");  #remove it
system ("touch $target");  #create it
print ("target file is :$target\n");



sub process_files {
	my $path =shift;  #get the argument
	opendir (DIR, $path) or die "unable to open $path: $!";
	my @files = grep{!/^\.{1,2}$/} readdir(DIR); #exclude . or .. directory
	closedir (DIR);
	@files = map{$path . '/' . $_} @files;
	for(@files) 
	{
		if (-d $_) 
		{
			process_files ($_);
		}
		else   # this a file we found
		{	
			
			my $mode = (stat($_)->mode) or (warn "$_ coun't get its mode\n"; next);	#if not succcess, warn and go to next element;
			$mode = $mode & 0777;
			$processedfs++;
			print "$processedfs: $_ : $mode\n";
			print Processedfiles "$processedfs: $_ : $mode \n";      #out put processed files 

			if ((index($_,$binStr)!=-1)   #find bin.x86_64 str
			     && ($mode & 111 == 111 )	#execution permission file          
			   ) #bin.x86_64 files and has execution permission 
			{		
					print color 'red';
					$wantedfiles++;
					print ($wantedfiles,": $_ : $mode\n");
					# only these executable files	 
					print FILE "wanted files found: $wantedfiles : $_ : $mode\n";
					print color 'reset';
					system("valgrind --tool=memcheck --leak-check=full $_ >>$target 2>&1  &"); 
			}	
		}
	}
}

print "\nValgrind Unit Tests Start...\n";
for (my $num =0; $num <3; $num++)
{
	print ("counting ....  " ,3-$num , "\n");
	sleep (1);
}	
print "\nValgrind Unit Tests now Start...\n";

#really starts recursively start#

open (FILE,'>>',"$target") or die "could not open $target : $!";  #append , if not exist, create a new file

open (Processedfiles,'>>',"$processedfiles") or die  "couldn't open the file : $!"; # open a file with open command

	process_files($dir);   

close Processedfiles;
close FILE;

my $editor ="vi ";	# finally we open the result file
my $pidEditor = fork();
if ($pidEditor ==0) {
my $editcmd = $editor . $target . " &";
system ($editcmd);
exit (0);
}

my $pidEditor2 = fork();
if ($pidEditor2 ==0) 
{
my $editcmd2 = $editor . $processedfiles . " &";
system ($editcmd2);
exit (0);
}













其中,使用到了sub routine : process_files, 具体代码如下:

<pre name="code" class="plain">sub process_files {
	my $path =shift;  #get the argument
	opendir (DIR, $path) or die "unable to open $path: $!";
	my @files = grep{!/^\.{1,2}$/} readdir(DIR); #exclude . or .. directory
	closedir (DIR);
	@files = map{$path . '/' . $_} @files;
	for(@files) 
	{
		if (-d $_) 
		{
			process_files ($_);
		}
		else   # this a file we found
		{	
			$processedfs++;
			print "$processedfs: $_\n";
			print Processdfiles "$processedfs: $_\n";
			if ((index($_,$binStr)!=-1) && (index($_,'.')==-1)) #bin.x86_64 files and not contains . 
			{		
					print color 'red';
					$wantedfiles++;
					print ($wantedfiles,": $_");
					print color 'reset';
					# only these executable files	 
					system("valgrind --tool=memcheck --leak-check=full $_ >> $target &");	
			}	
		}
	}
}
 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值