From: [email protected]
[mailto:[email protected]] On Behalf Of zilore
mumba
Sent: 14 April 2009 18:38
To: [email protected]
Subject: help concatenation of files
> Dear Perl Users,
> In the program below, am trying to put many text files into one. The
program creates the directories as
> desired, moves the files to the directories as per commands and
creates an output file.
> But the file created is empty and the error message is indicated as
the path specified is not found. Am not
> sure exactly which line is not correct.
> Help will be appreciated.
> Zilore Mumba
>
It would help greatly if you provided the exact error message, and
exactly what part of your script caused it. However there is your code
to comment on.
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
A good start.
> use POSIX;
You might want to change this to 'use POSIX qw{strftime};'. See below.
> use File::Path;
> use File::Copy;
> #
> my $debug = 0;
> #
> my @now = localtime;
> my ($day, $mon, $year) = (localtime(time() - 60 * 60 * 24) ) [
3,4,5];
>
> $year = $year+1900;
> $mon = $mon+1;
> open(DAT_OUT, ">datefile.txt");
You should always check whether the open was successful.
> printf DAT_OUT ("%04d%02d%02d\n", $year, $mon,$day);
> close OUT;
Its probably a good idea to check that the close for an output file
succeeded, because it will likely be here that you learn that your disk
is full.
Also, it's a good idea to close the file handle that you actually opened
and wrote to, not some random one.
> open(DAT_OUT, "<datefile.txt"); # we write date in a file so
that month is tow-digit
> my $dat0=<DAT_OUT>;
> chomp $dat0;
> close OUT;
Its not clear why you want to write a string to a file, only to read it
in again. Unless you actually need that file for something else, the
above code can be accomplished by:
my $dat0 = strftime "%Y%m%d", localtime(time - 24 * 60 * 60);
> my $msg_pref = 'msgs';
> my $ext ='.txt';
> my $out_file = "$dat0$msg_pref$ext";
> my $msg_dir = 'Messages';
> if ( ! -d $msg_dir ) { # Create a directory called Messages
> print "mkpath $dat0\n" if $debug;
> mkpath ($msg_dir ) or die "mkpath '$msg_dir' failed: $! ($^E)";
> }
>
> my $cmd01 = "mv msg* $msg_dir"; # Move all messages to directory
Messages
> print "doing system ($cmd01)\n" if $debug;
> system ($cmd01);
You need to check whether this command succseeded. See 'perldoc system'
for how. The same applies to following system commands.
> chdir ($msg_dir) or die "cd to '$msg_dir' failed: $! ($^E)";
> # Change directory to directory Messages
> if ( ! -d $dat0 ) {
> print "mkpath $dat0\n" if $debug;
> mkpath ($dat0) or die "mkpath '$dat0' failed: $! ($^E)";
> } # Create a directory called by yesterday's date
> # Move all files to date_directory
> my $cmd02 = "mv msg* $dat0"; # Move all messages to date directory
> print "doing system ($cmd02)\n" if $debug;
> system ($cmd02); #But program remains in Messages
> # 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)";
You never write to this file, or explicitly close it. That being the
case, this file is likely to be empty.
> my @files =();
> opendir DIR, "$dat0" or die "opendir '$dat0' Failed: $! ($^E)";
> while (my $file = readdir DIR) {
> next if $file =~ m/msg*/;
So you are ignoring all files in the date directory which contain the
string "ms" followed by zero or more "g"s. That is, by my reckoning, all
of the files that you previously moved into the date directory with the
command "mv msg* $dat0". Perhaps you mean "next unless $file =~ /^msg/;"
> print "Adding '$file'\n" if $debug;
> push(@files,$file);
> }
> closedir DIR;
> #my $cmd03 = "type @files > $out_file"; # for
Windows
> my $cmd03 = ("/cygdrive/c/progra~1/pcgrads/win32/cat @files >
$out_file");
> print "doing system ($cmd03)\n" if $debug;
> system ($cmd03);
You should probably check that the array @files is not empty before
attempting this command, which I believe is highly likely in this case.
Also, note that the output file created by this command will not be the
same as in the above open function. You use the same name, but write it
to a different directory.
> #my $cmd04 = "rm msg*";
> #print "doing system ($cmd04)\n" if $debug;
> #system ($cmd04);
> __END__
You are aware that everything from this point on will be treated as
comments by perl?
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