-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of zilore
mumba
Sent: 20 April 2009 11:52
To: [email protected]
Subject: Re: Concatenation of files
> Just in case you are able to spend another 5 five minutes for me, at
the end of writing the data in one file, I > need to remove the original
files. If I try to unlink the files I could end up deleting the newly
created file. > I do not want to mess this wonderful code.
If you don't need to keep the original files, why move them? Just copy
them to the concatenated file from the original directory, and delete
each file after copying.
use strict;
use warnings;
use POSIX;
use File::Path;
use File::Copy;
# Variable declaration
my $debug = 1;
my $from_dir = "C:/Temp";
my $top_dir = "C:/Weather";
my ($day, $mon, $year) = ( localtime(time - 86400) )[ 3, 4, 5 ];
my $dat0 = sprintf( "%04d%02d%02d", $year + 1900, ++$mon, $day );
my $out_file = "$top_dir/$dat0/$dat0.txt";
print "out_file='$out_file'\n" if $debug;
$dat0 = "$top_dir/$dat0";
# Create a directory called by yesterday's date if not there
if ( ! -d $dat0 ) {
print "mkpath $dat0\n" if $debug;
mkpath( $dat0 ) or die "mkpath '$dat0' failed: $! ($^E)";
}
# open output file - in dat0
print "Creating output file '$out_file'\n" if $debug;
open my $OUT, '>>', $out_file or die "Append to '$out_file: failed $!
($^E)";
opendir my $DIR, $from_dir or die "opendir failed on $from_dir: $!
($^E)";
# Copy all files to output file, and delete them
while ( my $item = readdir $DIR )
{
my $fn = "$from_dir/$item";
next if -d $fn;
# Extra filtering perhaps, to make sure we only copy message files
???
next unless $item =~ /^msg/;
print "Copying $fn\n" if $debug;
copy($fn, $OUT) or die "Failed to copy $fn: $!\n";
unlink $fn or die "Failed to delede $fn: $!\n";
}
closedir $DIR;
HTH
--
Brian Raven
This e-mail may contain confidential and/or privileged information. If you are
not the intended recipient or have received this e-mail in error, please advise
the sender immediately by reply e-mail and delete this message and any
attachments without retaining a copy.
Any unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs