My guess is that the file is open. MP3::Tag has different handlers for ID3v1 and ID3v2 formats, so my guess is that some of the "unrenamable" files have ID3v2-style tags and are kept open. Permission denied usually means that a file has been opened by a program and not yet closed, even if it is your own program.

You can try to close the file by discarding your $mp3 variable before trying to rename. Update: See bart's post below for the ->close method, which is likely less confusing than the discarding of the object just to close the filenhandle.

... my ($title, $track, $artist); { my $mp3 = MP3::Tag->new($curfile) or die "Didn't get an mp3 tag from '$curfile'"; ($title, $track, $artist) = $mp3->autoinfo(); }; ...

As an aside, rename() also takes directory names, so you don't need to chdir(). Have a look at File::Basename and/or File::Spec to get convenient pathname manipulation tools:

my $newname = File::Spec->catfile($mp3dir[$i],"$track. $artist - +$title.mp3"); my $oldname = File::Spec->catfile($mp3dir[$i],$mp3file[$i]); rename($oldname, $newname) or die "Couldn't rename '$oldname' to '$newname': $!";

In reply to Re: rename works sporadically on windows?? by Corion
in thread rename works sporadically on windows?? by johfiner

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.