0

我有大约 1000 个文件命名为

CU20130404160033.TXT CU20130405160027.TXT .... CUYYYYMMDDHHMMSS.TXT

我需要根据 min(date) 到 max(date) 的名称将所有文件附加到单个文件中

我怎样才能使程序高效,我应该根据文件的日期对文件进行排序并创建文件句柄。

opendir (DIR, $Directory) or die $!;

@files = grep { (!/^\./) && -f "$Directory/$_" } readdir(DIR);

chdir($Directory);

#create an array of open filehandles.
@fh = map { open my $f, $_ or die "Cant open $_:$!"; $f } @files;

#create new file for output

open $out_file , ">$filename" or die "cant open new file $!";
4

2 回答 2

2

像这样的东西应该会有所帮助。

opendir (DIR, $dir);
@dir=readdir(DIR);
closedir(DIR);

#Sort file list by modification time and write to an output file    
open(my $fh, ">", "output.txt") or die $!;
print $fh sort{ -M "$dir/$b" <=> -M "$dir/$a" }(@dir);
close $fh;
于 2013-06-27T11:42:43.770 回答
1

我建议这个脚本:

use strict;
use warnings;

#1. Set up the general values
my $directory = "... the dir ...";
my $out_file  = "... the out file ...";

#2. Fill an array with the names of your files
opendir(my $dh, $directory) or die $!;
while( my $file = readdir($dh) ) {
    push @files, $file;
}
closedir $dh;

#3. Sort the array 
@files = sort {$a cmp $b} @files;

#4. Open the target file
open $out_file , '>', $filename or die $!;

#5. Iterate for each input file, open it, 
#   and write line by line its contents to the target
foreach my $filename(@files) {
   open my $ifh, '<', $filename or die $!;
   while( my $line = <$ifh> ) {
      print $out_file $line;
   }
   close $ifh;
}

#6. Close the target
close $out_file;
于 2013-06-27T06:57:42.090 回答