Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#! 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 | |
|
Re: Program Automation
by djantzen (Priest) on Feb 20, 2003 at 03:09 UTC | |
by Anonymous Monk on Feb 20, 2003 at 03:27 UTC |