Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have coded this program in sections and it does work. Now I have to put it all together to make it work as one. Basically I'm reading a file, parse and insert it to another file. Then I read the text file and grab the directory path to search for Rich Text Format files within the directory. Since it is a file that contains many subdirectories and within them. There is Rich Text Format file that I need to get to copy them to another directory and subsequently rename them with a number and date that exist in the file. I also create a text file for each record I read with the parse file. I know this operation sounds crazy. However I have put it together and it is almost working. I could get the program to parse the file and pass PATHs to the other function to start grabbing the RTF files. But I'm stuck... I was wondering if you hav any suggestion in reference to the program functionality. Thanks
#! perl -w ###################################################### # Program to automate document extraction ###################################################### use strict; use File::Copy; use File::Spec::Functions qw(catfile); #################### # Read Template File #################### my $infile = 'c:/doclist.chr'; my $outfile = 'c:/doclist.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; ## print OUT join '|', split /,/ while <IN>; while(<IN>) { chomp; my @fields = split /,/; my $path_str = $fields[6]; do { warn "Empty field 7"; next } unless $path_str; my @path = split /\\/, $path_str; # assuming you want to remove a few directories my $fixed_path = join "\\", @path[0,5,6]; my $out = join '|', @fields[0..5], $fixed_path, "\n"; print OUT $out; } exit; close IN; ########################### # End of Read Template File ########################### ############################################# # Find, Copy, and Rename File to MRN+Date.rtf ############################################# my $now = `date`; foreach my $fetchdir ($fixed_path) { my $mrn = @fields[0]; opendir MYDIR, $fetchdir or die "Could not opendir $fetchdir: $!\n"; my @allfiles = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; closedir(MYDIR); my @files = grep { !-d } @allfiles ; my @dirs = grep { -d } @allfiles ; print @files." files and ".@dirs." directories in $fetchdir\n" ; print map "$_\n", @allfiles; my @select_files = grep /\.rtf\z/i, @files; for my $file (@select_files) { copy catfile($fetchdir,$file), catfile("C:\\temp", $file); } my $newdir = 'C:\\temp'; opendir MYDIR, $newdir or die "Could not opendir $newdir: $!\n"; my @all_files = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; closedir(MYDIR); my @change_files = grep { !-d } @all_files; foreach my $get_files (@change_files) { my $newfile = $get_files; $newfile =~ s/\$mrn.$now.rtf$/word1.rtf/; if (-e $newfile) { warn "can't rename $get_files to $newfile: $newfile exists\n"; } elsif (rename "$newdir/$get_files", "$newdir/$newfile") { print "file was renamed to $newfile\n" } else { warn "rename $get_files to $newfile failed: $!\n"; } } } close OUT; #################################### # End of Find, Copy, and Rename File #################################### ############################### # Create Text File per each MRN ############################### foreach my $text_file (@fields[0]) { if ($mrn = @fields[0]) { my $newpatientrec = $newfile; open NEWPATIENT,">$newfile" or die "Couldn't open $outfile, $ +!"; close NEWPATIENT; } } ######################### # End of Create Text File #########################

Replies are listed 'Best First'.
Re: Program Automation
by tall_man (Parson) on Feb 20, 2003 at 04:44 UTC
    There's a serious barrier in there that will prevent the pieces from working together: the exit statement a couple of lines before the comment "# End of Read Template File". That will end your program right there. Suppose you got rid of that and made a few subroutines (note -- this is untested code).

    Updates were made a few times here, but I'm finished now:

    #! perl -w ###################################################### # Program to automate document extraction ###################################################### use strict; use File::Copy; use File::Spec::Functions qw(catfile); sub read_template_file { # Might as well make the infile and outfile parameters. my $infile = shift; my $outfile = shift; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; ## print OUT join '|', split /,/ while <IN>; while(<IN>) { chomp; my @fields = split /,/; my $path_str = $fields[6]; do { warn "Empty field 7"; next } unless $path_str; my @path = split /\\/, $path_str; # assuming you want to remove a few directories my $fixed_path = join "\\", @path[0,5,6]; my $out = join '|', @fields[0..5], $fixed_path, "\n"; print OUT $out; # CRITICAL POINT: if you want to do other actions # on @fields or $fixed_path, you must do them here. # The values of the variables will be overwritten each # pass through the loop. find_copy_rename($fixed_path, $fields[0]); create_text_file($fields[0]); } close IN; # Moved here after the loop. close OUT; } sub find_copy_rename { # There used to be a loop here, but it didn't make # sense because it was looping over one item. my ($fetchdir, $mrn) = @_; my $now = `date`; opendir MYDIR, $fetchdir or die "Could not opendir $fetchdir: $!\n"; my @allfiles = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; closedir(MYDIR); my @files = grep { !-d } @allfiles ; my @dirs = grep { -d } @allfiles ; print @files." files and ".@dirs." directories in $fetchdir\n" ; print map "$_\n", @allfiles; my @select_files = grep /\.rtf\z/i, @files; for my $file (@select_files) { copy catfile($fetchdir,$file), catfile("C:\\temp", $file); } my $newdir = 'C:\\temp'; opendir MYDIR, $newdir or die "Could not opendir $newdir: $!\n"; my @all_files = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; closedir(MYDIR); my @change_files = grep { !-d } @all_files; foreach my $get_files (@change_files) { my $newfile = $get_files; $newfile =~ s/\$mrn.$now.rtf$/word1.rtf/; if (-e $newfile) { warn "can't rename $get_files to $newfile: $newfile exists\n"; } elsif (rename "$newdir/$get_files", "$newdir/$newfile") { print "file was renamed to $newfile\n" } else { warn "rename $get_files to $newfile failed: $!\n"; } } } sub create_text_file { # I'm not sure what belongs here. The existing code # does not make much sense to me. # Again you were looping over one thing. # You tested an assignment statement, probably a bug. # And you opened a file for output and didn't write to it. } read_template_file('c:/doclist.chr','c:/doclist.txt');
Re: Program Automation
by djantzen (Priest) on Feb 20, 2003 at 03:09 UTC

    Rather than trying to combine these already disparate chunks of functionality into a single monolithic block, why not break them out into separate subroutines? Smaller, simpler pieces of code are easier to read, maintain and debug. Your main script then only has to be a few lines long, calling the subroutines in sequence, passing the output from the previous call into the next.


    "The dead do not recognize context" -- Kai, Lexx
      I appreciate your response, but I'm fairly new to Perl. Perhaps if you could suggest how could I modify my code properly. I'll appreciate your help.