Hello Monks,
This program finds a file in a directory then it copies to another directory. it works find but I'm trying to rename the file after it finishes copying to the destination directory. The thing is that I read a file and grab the first field[0] which contains the "String" that rename the file (for example 123456.rtf), then I grab field
13 which contains the path of the directory that I'm grabbing the file from (for example C:\abc\mydir) I'm not very familiar to do the rename procedure after copying the file. I could grab the file out the source directory but cannot rename after copying the file to destination directory because it only copies one file and doesn't continue reading the file. Do you have any ideas to accomplish this task?
Here is my code
#! perl -w
use strict;
use File::Copy;
my $infile = 'c:/doclist1.chr';
open IN, "<$infile" or die "Couldn't open $infile, $!";
while (<IN>) {
chomp;
my @fields = split /,/;
my $mrn = $get_mrn[0];
my $path_str = $fields[13];
do { warn "Empty field 13"; next } unless $path_str;
my @path = split /\\/, $path_str;
my $dir = join "\\", @path[ 0, 5, 6 ];
process_dir($dir);
}
close IN;
sub process_dir {
my $dir = shift;
do { warn "$dir does not exist!\n"; return } unless -e $dir;
opendir DIR, $dir or do { warn "Could not open $dir $!\n" ; return
};
while ( my $file = readdir DIR ) {
next unless -f "$dir\\$file";
next unless $file =~ m/\.rtf$/;
copy( "$dir\\$file", "C:\\testfiles\\$file" )
or die "Failed to copy $file: $!\n";
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.