473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Perl rename function doesnt work!

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
2 3479
KevinADC
4,059 Expert 2GB
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
eggi
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

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

Similar topics

18
by: Mike | last post by:
I'm fairly new to PHP, but a long time Perl coder. This is probably answered somewhere, but I haven't been able to find it. The "strength" behind Perl is in its modules; are there modules...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
4
by: GO | last post by:
I have a custom Perl application (programmed by myself) that is used to rename files. For some reason, within the last month, it is no longer working properly. Prior to this I've been using it for...
3
by: Hugz | last post by:
Hello, And Thank you for taking time to help me.Myself Hugz.I am new to perl and want to write my own Scripts.But i can't understand how perl "rename" function and "file locking" works. ...
3
by: Mark | last post by:
Hello, I am new to PERL and I want to do a simple function (in LINUX): find all files that have the extension pseudo.pdf and put the file names in a file; parse the file and change all file...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
12
by: rurpy | last post by:
Is there an effcient way (more so than cgi) of using Python with Microsoft IIS? Something equivalent to Perl-ISAPI?
12
by: mantrid | last post by:
Im trying to move a file but am having luck my code is below. The temp and target paths are valid as they echo correctly. but I cant get the copy() function to work, or the rename() function ...
0
by: bdan44 | last post by:
HI, I have a perl script that I want to call in java. I can call it, the only problem is that the perl script has <STDIN> and the perl sciprt doesn't work. Other perl scripts(without <STDIN>)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.