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

Hi monks, I am trying to rename file across different directories and I am getting a "Is a directory" error when I shouldn't be. Here is a snippet of my code.
#!/usr/bin/perl -w use File::Copy; use File::Path; use strict; use warnings; .... /* $filename, $pat_name, $study_date, $study_time, $series_num are define +d */ .... my ($new_filename, $dir_path); #create new pathname $dir_path = "$pat_name/$study_date-$study_time/$series_num"; #create new filename $new_filename="$pat_name-$study_date-$study_time-$series_num-$image_nu +m.dcm"; eval { mkpath($dir_path) }; if ($@) { print "Couldn't create $dir_path: $@"; } $new_filename = "$dir_path/$new_filename"; rename($filename,$new_filename) or die( "could not rename $filename to + $new_filename: $!\n" );
Instead of the rename, I have tried:
system("mv",$filename,$new_filename) or die("could not rename $filenam +e to $new_filename: $!\n");
or
move($filename,$new_filename) or die("could not rename $filename to $n +ew_filename: $!\n");
with no luck. Here is a sample print out:
could not rename 1.2.840.113619.2.55.1.1762883246.3061.1000220985.209/ +1.2.840.113619.2.55.1.1762883246.3061.1000220985.210/1.2.840.113619.2 +.55.1.1762883246.3061.1000220985.212.dcm to S000109A33/20010911-12560 +2/1/S000109A33-20010911-125602-1-2.dcm: Is a directory
It is not a question of permission because permissions are set as a+rwx. Any help will be appreciated. I have been struggling with this for a while. I am using Perl 5.8.5.

---- UPDATE -----

I think the error is caused by the mkpath function. The directory structure is not created. I tried:
if( ! -e $dir_path) { system("mkdir","-p",$dir_path); }
I also tried writing my own recursive mkdir subfunction, but the -e and -d misbehave and claims a folder exists when it doesn't. With this code:
system("mkdir","-p",$dir_path) or warn("could not make dir $dir_path: +$!\n");
I get an "Illegal seek" error. Please help! Thank you.

---- UPDATE -----

I manually created the directory structure for the new filename and reran the script. Getting the rename error.

Replies are listed 'Best First'.
Re: Weird rename error
by GrandFather (Saint) on Jul 26, 2005 at 20:42 UTC

    If you call mkpath with a non-zero second parameter like eval { mkpath($dir_path, 1) };, it will "print the name of each directory as it is created" which may help diagnose the problem.


    Perl is Huffman encoded by design.
      I tried that, and it printed out the directories that its suppose to create:
      mkdir S000471M37 mkdir S000471M37/20020206-091042 mkdir S000471M37/20020206-091042/268 could not rename 1.2.124.113532.172.16.48.200.20010514.120225.267932/1 +.2.840.113619.2.80.2162541498.1619.1046210641.2/1.2.840.113619.2.80.2 +162541498.1619.1046210641.3.dcm to S000471M37/20020206-091042/268/S00 +0471M37-20020206-091042-268-1.dcm: Is a directory
      but after the program dies
      ls -R S000471M37/ S000471M37/:
      It only likes to create the first level directory.
        What version of File::Path and File::Copy do you have?