Justin,
Thank you very much, the \n in sprintf was precisely the error. Now the script 
does as required. Incidentally am more novice to Perl than you. I have only 
read Perl in tutorials and practising it is not easy, I have learnt a lot 
through this forum.
Two minor points though:
1.in moving the files, the script tries to move itself to the date directory 
but fails saying the file is in use.
2.At the end of the process, I would like to delete the individual files. The 
part of the code below does not do this, saying "no such path or directory".
Thanks
 
opendir DIR, $dat0 or die "opendir $dat0: $! ($^E)";
while ($_ = readdir DIR) {
    next if -d $_;
    print "unlink $_";
    unlink $_, or warn "unlink $_' failed: $! ($^E)";
}
closedir DIR;




--- On Fri, 4/17/09, Justin Allegakoen <[email protected]> wrote:

> From: Justin Allegakoen <[email protected]>
> Subject: RE: Concatenation of files
> To: [email protected]
> Date: Friday, April 17, 2009, 11:56 AM
> > -----Original Message-----
> > From: [email protected]
> [mailto:activeperl-
> > [email protected]] On Behalf Of zilore
> mumba
> > Sent: Friday, 17 April 2009 4:31 PM
> > To: [email protected]
> > Subject: Concatenation of files
> > 
> > 
> > Dear Perl Community,
> > Based on the very constructive comments I received
> from many, and on
> > the script provided by Bill I have modified my script
> as below. This
> > time I create only one (date) directory and move all
> files (in current
> > directory)there and concatenate them. I am getting an
> error in creation
> > of dtae directory and cannot figure out the source of
> error. I have
> > indicated by ### where the error occurs.
> > Help appreciated
> > Good day
> > Zilore
> > 
> > #!/usr/bin/perl --
> > use strict;
> > use warnings;
> > use POSIX;
> > use File::Path;
> > use File::Copy;
> > my $debug = 1;
> > my @now = localtime;
> > my ($day, $mon, $year) = (localtime (time -
> 86400))[3,4,5];
> > $year = $year + 1900;
> > $mon = $mon + 1;
> > my $dat0 = sprintf ("%04d%02d%02d\n",
> $year, $mon, $day);
> > my $ext = '.txt';
> > my $out_file = "$dat0$ext";
> > print "out_file='$out_file'\n"
> if $debug;
> > 
> > # Create a directory called by yesterday's date if
> not there
> > if ( ! -d $dat0 ) {                ### Error here
> >     print "mkpath $dat0\n" if $debug;
> >     mkpath ($dat0) or die "mkpath '$dat0'
> failed: $! ($^E)"; ### €rror
> > here
> > }
> > # Move all files to date_directory
> > opendir DIR, '.' or die "opendir
> '.': $! ($^E)";
> > while ($_ = readdir DIR) {
> >     next if -d $_;
> >     print "rename $_,
> '$dat0/$_'\n";
> >     rename $_, "$dat0/$_" or warn
> "rename '$dat0/$_': $! ($^E)";
> > }
> > closedir DIR;
> > # open output file - in dat0
> > print "Creating output file
> '$out_file'\n" if $debug;
> > open OUT, ">>$dat0/$out_file" or die
> "Create '$out_file: failed $!
> > ($^E)";
> > opendir DIR, $dat0 or die "opendir
> '$dat0': $! ($^E)";
> > while ($_ = readdir DIR) {
> >     next if -d $_; next if /^$out_file$/i;
> >     print "Adding '$_'\n" if
> $debug;
> >     open IN, "$dat0/$_" or warn "open
> '$dat0/$_': $! ($^E)";
> >     while (<IN>) {
> >         print OUT;
> >     }
> >     close IN;
> > }
> > closedir DIR;
> > close OUT;
> > __END__
> > 
> 
> Looks like it's to do with the \n in your sprintf -
> take it out like so:-
> 
> my $dat0 = sprintf ("%04d%02d%02d", $year, $mon,
> $day);
> 
> You've got $debug enabled yet I'm not sure why you
> can't pick up that special chars in a file name are
> unacceptable:-
> 
> out_file='20090416
> .txt'
> 
> As a beginner I'd recommend that you add a few lines of
> code at a time and then test it straight away. Carefully
> looking at the output.
> 
> Been a long day has it?
> 
> I didn’t check the rest of the code, but I assume the
> removal of \n will be enough.
> 
> Just in
> 
> 
> _______________________________________________
> ActivePerl mailing list
> [email protected]
> To unsubscribe:
> http://listserv.ActiveState.com/mailman/mysubs


      
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to