By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,282 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,282 IT Pros & Developers. It's quick & easy.

Perl rename function doesnt work!

P: 1
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!!
Expand|Select|Wrap|Line Numbers
  1. $dirname = "C:/folder";
  2.  
  3. opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
  4. while (defined($file = readdir(DIR))) {
  5.  
  6. $neu = $file;
  7. print $neu;
  8. print "\n";
  9.  
  10. $neu =~ tr/['&a-zA-Z]/ /c;
  11. print $neu;
  12. print "\n";
  13.  
  14. $neu =~ tr/A-Z/a-z/;
  15. print $neu;
  16. print "\n";
  17.  
  18. $neu =~ s/\b(\w+)\b/ucfirst($1)/ge;
  19. print $neu;
  20. print "\n";
  21.  
  22. $neu =~ s/Mp/.mp3/;
  23. $neu =~ s/Wav/.wav/;
  24. $neu =~ s/Fl/.flv/;
  25. $neu =~ s/Vob/.vob/;
  26. $neu =~ s/'S/'s/g;
  27.  
  28. print $neu;
  29. print "\n";
  30.  
  31. $neu =~ tr/ //d;
  32. print $neu;
  33. print "\n";
  34.  
  35. rename ("$file", "$neu");
  36.  
  37. }
  38.  
  39. #rename "C:/folder/03-kt_tunstall-one_day.mp3", "C:/folder/NEW NAME.mp3";
  40.  
  41. closedir(DIR);
Apr 1 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
replace this line:

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

with:

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

and see what the values of $file and $neu are. They are most likely just filenames with no directory path in them. You will probnably need to add the directory path.
Apr 1 '08 #2

P: 9
Hmm... that's odd.

I created a file named "binger", ran the inside of your code and it did rename the file to "Binger."

I would do as was suggested in the previous reply. It seems that the problem is most likely that the $file variable isn't getting set correctly or there are characters i teh name that arent' getting translated with "tr" properly. If you are using full path names, you may need to specifically account for the "/" characters.

Hope that helps :)

, Mike

Expand|Select|Wrap|Line Numbers
  1.  #!/usr/bin/perl 
  2. $file = "binger";
  3. $neu = $file;
  4. print $neu;
  5. print "\n";
  6. $neu =~ tr/['&a-zA-Z]/ /c;
  7. print $neu;
  8. print "\n";
  9. $neu =~ tr/A-Z/a-z/;
  10. print $neu;
  11. print "\n";
  12. $neu =~ s/\b(\w+)\b/ucfirst($1)/ge;
  13. print $neu;
  14. print "\n";
  15. $neu =~ s/Mp/.mp3/;
  16. $neu =~ s/Wav/.wav/;
  17. $neu =~ s/Fl/.flv/;
  18. $neu =~ s/Vob/.vob/;
  19. $neu =~ s/'S/'s/g;
  20. print $neu;
  21. print "\n";
  22. $neu =~ tr/ //d;
  23. print $neu;
  24. print "\n";
  25. rename ("$file", "$neu");
  26.  
Apr 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.