ActiveState Powered by ActiveState

ActiveState Community


rename function acts weird!

Posted by tanya_567 on 2008-03-31 10:46

Hello you helpful folk! Please look at my script and tell me what I am doing wrong! I am cleaning the names of my music files from unnecessary symbols, and the cleaning works fine, however the perl rename function fails to do the actual renaming. I tried to rename a file in the same dir using strings instead of variables and that works!! But it seems I need to get it to work with vars as well. Can anyone give me a clue? Thanks!!

$dirname = "C:/folder";

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {

$neu = $file;
print $neu;
print "\n";

$neu =~ tr/['&a-zA-Z]/ /c;
print $neu;
print "\n";

$neu =~ tr/A-Z/a-z/;
print $neu;
print "\n";

$neu =~ s/\b(\w+)\b/ucfirst($1)/ge;
print $neu;
print "\n";

$neu =~ s/Mp/.mp3/;
$neu =~ s/Wav/.wav/;
$neu =~ s/Fl/.flv/;
$neu =~ s/Vob/.vob/;
$neu =~ s/'S/'s/g;

print $neu;
print "\n";

$neu =~ tr/ //d;
print $neu;
print "\n";

rename ("$file", "$neu");

}

#rename "C:/folder/03-kt_tunstall-one_day.mp3", "C:/folder/NEW NAME.mp3";

closedir(DIR);

FlipBumWalla | Wed, 2008-04-23 13:32

I am experiencing the same thing in Windows XP Pro. Renaming a file by 'hard-coding' the name works. Renaming a file based a variable does not.

Any suggestions? Any hints?

Hi
tanya_567 | Thu, 2008-04-24 04:01

Mine worked. Here is how:

$file = "C:/folder1/" . "$file";
$neu = "C:/folder1/" . "$neu";

if (!rename($file, $neu )) {
print $!, "\n";
}

You just need to add what it takes to have the full path for the file names.

Because, if you read the files initially with readdir, logically that only gives you the shortened file name since it reads in a particular directory. Hope that helps.

-->