Hello Monks, I really need your help. I have been working with Perl for the past few month but I'm not an expert. I'm seeking your expersite on this task. Basically, I open and read a text file, then I start to read it line by line. Then I replace commas by pipes and grab the path to format it; then I pass the newly created path to a loop to start finding RTF files that belong ro each directory that I'm searching on. Then copy and rename the file in a specify format. I also create a text file with each line from the parse file. I'll appreciate your help.
BEFORE 484257-1,DE ARMAS,BLANC,Cristiane Takita,1/21/03,MD Wkly Note,H:\APPS\ +IMPAC\DB\ESCRIBE\00\00004EAC.006 467488,FOURNIER,JANET,Cristiane Takita,1/22/03,MD Wkly Note,H:\APPS\IM +PAC\DB\ESCRIBE\12\00004E04.012 306684,SECHI,OSVAL,Aaron Wolfson,1/22/03,MD Wkly Note,H:\APPS\IMPAC\DB +\ESCRIBE\08\00004E14.022 310941,PAUL,TAMEA,B-Chen Wen,1/22/03,MD Wkly Note,H:\APPS\IMPAC\DB\ESC +RIBE\01\00004E35.009
AFTER
484257|DE ARMAS|BLANC|Cristiane Takita|1/21/03|MD Wkly Note|H:\00\0000 +4EAC.006 467488|FOURNIER|JANET|Cristiane Takita|1/22/03|MD Wkly Note|H:\12\0000 +4E04.012 306684|SECHI|OSVAL|Aaron Wolfson|1/22/03|MD Wkly Note|H:\08\00004E14.0 +22 310941|PAUL|TAMEA|B-Chen Wen|1/22/03|MD Wkly Note|H:\01\00004E35.009
Desired output Newly created text file / each per line parse
310941|PAUL|TAMEA|B-Chen Wen|1/22/03|MD Wkly Note@@H:\01\00004E35.009\ +31094100004E35009.rtf
Newly created file 31094100004E35009.rtf
#! perl -w ########################################## # Program to automate document extraction ########################################## use strict; #################### # Read Template File #################### sub main { 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]; &find_copy_rename; &create_text_file; } exit; close IN; } ########################### # End of Read Template File ########################### ############################################# # Find, Copy, and Rename File to MRN+Date.rtf ############################################# use File::Find; use File::Copy; sub find_copy_rename { my @dir = $fixed_path; my $dir = @dir; if ($File::Find::dir ne $dir) { $File::Find::prune = 1; return 0; } return 0 if ($_ !~ /\.rtf$/); copy($File::Find::name, "C:\\temp\\$_") or die "Failed to copy $_: + $!\n"; return 1; } find(\&process_files, $dir); 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"; } } ############################### # Create Text File per each MRN ############################### sub create_text_fle { foreach my $text_file (@fields[0]) { if ($mrn = @fields[0]) { my $newpatientrec = $newfile; open NEWPATIENT,">$newfile" or die "Couldn't open $outfile, $ +!"; close NEWPATIENT; } my $out = join '|', @fields[0..5], $fixed_path, "\n"; print OUT $out; } }

In reply to Open File and Parse file line by line by skyler

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.