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.

In reply to Weird rename error by datmrman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.