perl读写文件

1,从命令行读取文件或者文件夹
2,支持无限多个文件或者文件夹参数,不分顺序
3,可以识别非法文件和文件夹,退出并提示

组合一:

FileDirHelper.pm


#!/usr/local/bin/perl
#Ljl
package FileDirHelper;

my ($size, $dircnt, $filecnt) = (0, 0, 0);
my @files;
my @AllDirs;

sub getAllFiles{
	my $dir=shift;
	lsr_s($dir);
	print "Load $filecnt files, $dircnt directory. $size bytes.\n";
	return @files;
}

sub getAllDirs{
	my $dir=shift;
	lsr_s($dir);
	return @AllDirs;
}

sub getDirDetail{
	my $dir=shift;
	lsr_s($dir);
	return ($size, $dircnt, $filecnt);
}

sub lsr_s($) {
    my $cwd = shift;
    my @dirs = ($cwd.'/');

    my ($dir, $file);
    while ($dir = pop(@dirs)) {
        local *DH;
        if (!opendir(DH, $dir)) {
            warn "Cannot opendir $dir: $! $^E";
            next;
        }
        foreach (readdir(DH)) {
            if ($_ eq '.' || $_ eq '..') {
                next;
            }
            $file = $dir.$_;         
            if (!-l $file && -d _) {
                $file .= '/';
                push(@dirs, $file);
            }
            process($file, $dir);
        }
        closedir(DH);
    }
}



sub process($$) {
    my $file = shift;    
    if(-f $file){
    	#print $file, "\n";
    	push @files,$file;
    }else{
      push @AllDirs,$file;
    }
    if (substr($file, length($file)-1, 1) eq '/') {
        $dircnt++;
    }
    else {
        $filecnt++;
        $size += -s $file;
    }
}

1;


AllFilesChecker.pl

#!/usr/local/bin/perl
#ljl
use Cwd;
use File::Basename;
use FileDirHelper;

if(scalar(@ARGV)<1){
	print "Usage: Please input the RD path/file\n";
	exit;
}

my @file;

for my $arg(@ARGV){
	if(not -e "$arg"){
		print "ERROR: ".$arg."  is not right path or file!\n";
		exit;
	}
	if(-f "$arg"){
	  push @file,$arg;	
	}else{
		my @fileTemp = FileDirHelper::getAllFiles($arg);
	  push @file ,@fileTemp;	
	}
}

if(!-d "result"){
	mkdir "result";
}

my $currPath = cwd();
my $outFile = $currPath.'/result/';

for my $file(@file){
 my $fileCount=0;
 my $filename=basename($file);
 #my $baseName=substr($filename,0,index($filename,'.'.'txd')); 
 #open OUT ,">".$outFile.$baseName.'.txt';
 print "[Read the file]: ".$filename."\n";
 open FILE, $file or die "can not open the file ".$filename."\n";
 while(<FILE>){
 	chomp;
 	print "\r\tProcess:".($fileCount++);
 	
 }	
 print "\n";
 close FILE;
}

#open OUT ,">".$outFile."result.txt";




print "Finish! You can check the result files in :".$outFile."\n";



组合二:

FindAllFiles.pm

package FindAllFiles;
sub getfile{
	my ($path,$expandName) = @_;
	undef @allFiles;
	my @allFiles=getFilesFromDir($path,$expandName);
	return @allFiles;
}
sub getFilesFromDir{
	my ($path,$expandName) = @_;
	@allFiles;
	opendir (PATH,"$path") or die "serious dainbramage:$!\n";
	my @subAll = grep !/^\.\.?$/,readdir PATH;
	closedir PATH;
	foreach my $file(@subAll){	
		my $subPath = "$path\\$file";
		if(-d "$subPath"){
			&getFilesFromDir($subPath,$expandName);
		} else {
			if(-e "$subPath" and $subPath =~ /$expandName$/){
				if($subPath=~/^(\\\\.*)(\\\\)(.*)$/){
					$subPath = $1."\\".$3;
				}
				push @allFiles,"$subPath";
			}
		}
	}
	return @allFiles;
}
1;

AllFilesChecker.pl

use Cwd;
use File::Basename;
use FindAllFiles;

if(scalar(@ARGV)<1){
	print "Usage: Please input the RD path/file\n";
	exit;
}
my $fileType='txd';#注意文件类型
my @file;

for my $arg(@ARGV){
	if(not -e "$arg"){
		print "ERROR: ".$arg."  is not right path or file!\n";
		exit;
	}
	if(-f "$arg"){
	  push @file,$arg;	
	}else{
		my @fileTemp = FindAllFiles::getfile($arg,$fileType);
	  push @file ,@fileTemp;	
	}
}

if(!-d "result"){
	mkdir "result";
}

my $currPath = cwd();
my $outFile = $currPath.'/result/';

for my $file(@file){
 my $fileCount=0;
 my $filename=basename($file);
 my $baseName=substr($filename,0,index($filename,'.'.$fileType));
 
 print "[Read the file]: ".$filename."\n";
 open FILE, $file or die "can not open the file ".$filename."\n";
 while(<FILE>){
 	chomp;
 	print "\r\tProcess:".($fileCount++);
 	
 }	
 print "\n";
 close FILE;
}

open OUT ,">".$outFile."result.txt";




print "Finish! You can check the result files in :".$outFile."\n";
print "*****************[Map Data]: The best group in the world! *******************\n";


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值