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

Dear Monks, I'm trying to compiled the code but it gave me an error. I put a print statement before the next statements so I could get an accurate result on what the script is doing. This is the error :
dir: C:\Apps\Impac\Db\Escribe\07\00002A0F.002 file: newfile:270917 dir: C:\Apps\Impac\Db\Escribe\07\00002A0F.002 file:STATUS.TXT newfile: 270917 dir: C:\Apps\Impac\Db\Escribe\07\00002A0F.002 file:WORD1.RTF newfile:2 70917 Failed to copy Word1.rtf: No such file or directory
I'm trying to copy the RTF file and not the TEXT . I cannot tell if the script is getting mix up with trying to rename the RTF and TEXT. Could you take a look at the code and let me know if I need to change anything to make it work. Thanks in advance
#! perl -w use strict; use File::Copy; my $infile = "c:/zip files misc/doclisth.chr"; open IN, "<$infile" or die "Couldn't open $infile, $!"; while (<IN>) { chomp; my @fields = split /,/; my $newfile = $fields[0]; my $path_str = $fields[13]; do { warn "Empty field 14"; next } unless $path_str; my @path = split /\\/, $path_str; my $dir = join "\\", @path[ 0, 1, 2, 3, 4, 5, 6 ]; process_dir($dir,$newfile); } close IN; sub process_dir { my ($dir, $newfile) = @_; 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 ) { print "dir: $dir file:$file newfile:$newfile\n"; #before the next unless statements. next unless -f "$dir\\$file"; next unless $file =~ m/\.rtf$/; copy( "$dir\\$file", "C:\\testfiles\\$newfile" ) or die "Failed to copy $file: $!\n"; } }

Replies are listed 'Best First'.
Re: Use file variables to copy and rename file.
by moxliukas (Curate) on Jun 04, 2003 at 21:39 UTC

    The only explanation for the above error that I can think of is that the directory C:\testfiles\ does not exist or is read-only and therefore the script cannot copy files there.

    Of course there might be some other explanation that someone will point out, but that's the only thing I can think of.

    Hope this helps

Re: Use file variables to copy and rename file.
by Skeeve (Parson) on Jun 05, 2003 at 05:39 UTC
    I'm wondering how often Skyler or Skyler99 is going to show us his code...

    Sorry for being a bit off topic.

    Skyler or Skyler99, try the perl debugger an see how the program works. Maybe you have a fundamental misunderstanding and the use of the debugger might help you.

    Call it with
        perl -d your-program's-filename

    Then you step through it by hitting once
        s[RETURN]
    and just
        [RETURN]
    for each next step. From time to time try a
        x $your-variable's-name
    to see what's in your variables.

    You stop debugging by entering
        q.

    HTH