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

Dear Monks, I wrote this program to find a file, copy it and rename it in the destination directory. It finds the file but doesn't copy or rename the file. There is no errors when I run the script. could you take a look at it and let me know what I'm doing wrong. I'll appreciate your feedback. Here is my code:
#! perl -w use strict; use File::Copy; my $infile = 'c:/doclisth.chr'; open IN, "<$infile" or die "Couldn't open $infile, $!"; while (<IN>) { chomp; my @fields = split /,/; my $mrn = $fields[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,$mrn); } close IN; sub process_dir { my $dir = shift; my $mrn = 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$/; my $newname = rename ($file, $mrn); copy( "$dir\\$file", "C:\\testfiles\\$newname" ) or die "Failed to copy $file: $!\n"; } }

Replies are listed 'Best First'.
Re: Cannot Copy and Rename file
by cciulla (Friar) on Jun 03, 2003 at 01:34 UTC

    I think your problem is here:

    my $newname = rename($file, $mrn);

    "rename" returns a "true" or "false" based upon success.

    A reply falls below the community's threshold of quality. You may see it by logging in.