perl读写文件
发布时间:2020-12-16 00:11:19 所属栏目:大数据 来源:网络整理
导读:1,从命令行读取文件或者文件夹 2,支持无限多个文件或者文件夹参数,不分顺序 3,可以识别非法文件和文件夹,退出并提示 组合一: FileDirHelper.pm #!/usr/local/bin/perl#Ljlpackage FileDirHelper;my ($size,$dircnt,$filecnt) = (0,0);my @files;my @Al
1,从命令行读取文件或者文件夹 组合一: FileDirHelper.pm #!/usr/local/bin/perl #Ljl package FileDirHelper; my ($size,$dircnt,$filecnt) = (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,$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/filen"; 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,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 "rtProcess:".($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/filen"; 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,'.'.$fileType)); print "[Read the file]: ".$filename."n"; open FILE,$file or die "can not open the file ".$filename."n"; while(<FILE>){ chomp; print "rtProcess:".($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"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |