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 #########################

In reply to Program Automation by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.