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

Renaming files

P: 55
Hi All,
I am trying to organize my song directory and trying to remove the duplicate entries, I have written a small code but somehow I am not able to rename the song name.
I am trying to change all the song name to uppercase and then removing the spaces, any number and other characters.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $cnt = 1;
  6. my $folder = "/home/kk/song/latest";
  7. my @files = `ls $folder/*.mp3 *.MP3`;
  8. my @sorted = sort { lc($a) cmp lc($b) } @files;
  9.  
  10. foreach my $file(@sorted)
  11. {
  12.     my $name = $file;
  13.     $name=~ tr/a-z/A-z/;
  14.     $name=~ s/\.MP3//g;
  15.     $name=~ s/\.mp3//g;
  16.     chomp $name;
  17.     $name=~ s/_/ /g;
  18.     $name=~ s/__/ /g;
  19.     $name=~ s/[0-9]//g;
  20.     $name=~ s/\s+/ /g;
  21.     $name=~ s/\(.*\)//g;
  22. #    print "$name\n";
  23.  
  24.     my $newname = $name.".MP3";
  25.     system 'mv',"$file", "/home/kk/song/latest/$newname";
  26.  
  27. #    print "$newname\n";
  28.     $cnt++;
  29. }
  30. print "$cnt\n";
  31.  
  32.  
I also want to remove the duplicate songs but couldn't figure out how to apply it??

Thanks for the help

Kumar
Jan 8 '09 #1
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
@kumarboston
For renaming files, you can use the system mv command or use the rename command. Either way, check out the perlfaq.

Also, with regards to duplicate files, since you cannot have two files in the same directory with the same name, I assume that you have files with the same name in different directories. If that is the case, you can simply unlink one of the files, which essentially deletes it.

Regards,

Jeff
Jan 8 '09 #2

P: 55
thanks Jeff for the reply,
I tried to give a new directory path for the rename files but it showed error that "cannot stat.....no such file or directory".


Thanks
Kumar
Jan 8 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
Untested code, Try on a test driectory of files before using on your important files. Ask questions if necessary.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $cnt = 1;
  6. my $folder = "/home/kk/song/latest";
  7. chdir($folder) or die "$!";
  8. opendir(DIR , '.') or die "$!"; 
  9. my @files = grep{/\.mp3$/i} readdir DIR;
  10. closedir DIR;
  11. chomp(@files);
  12. foreach my $old (@files){
  13.     my $new = $old;
  14.     $new =~ tr/0-9//d;
  15.     $new =~ s/_+/ /g;
  16.     $new =~ s/\s+/ /g;
  17.     $new =~ s/\(.*\)//g;
  18.     $new = uc($new);
  19. #    print "$new\n";
  20.     rename($old,$new) or die "$!";
  21. #    print "$new\n";
  22.     $cnt++;
  23. }
  24. print "$cnt\n";
  25.  
Jan 8 '09 #4

P: 55
Thanks Kevin for your help.
One last thing, i would like to seek your help. How to insert the code to remove the duplicate entries of song in the same code.
Here is the working code.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $cnt = 1;
  6. my $folder = "/home/kk/song/latest";
  7. chdir($folder) or die "$!";
  8. opendir(DIR , '.') or die "$!";
  9. my @files = grep{/\.mp3$/i} readdir DIR;
  10. closedir DIR;
  11.  
  12. chomp(@files);
  13.  
  14. foreach my $old (@files){
  15.     my $new = $old;
  16.     $new =~ s/\.mp3//g;
  17.     $new =~ tr/0-9//d;
  18.     $new =~ s/\s-\s//g;
  19.     $new =~ s/^\s+//g;
  20.     $new =~ s/_+/ /g;
  21.     $new =~ s/\s+/ /g;
  22.     $new =~ s/\(.*\)//g;
  23.     $new =~ s/!+\w+//g;
  24.     $new = uc($new);
  25.     $new = $new.".MP3";
  26. #    print "$new\n";
  27.     rename($old,$new) or die "$!";
  28.     print "$new\n";
  29.     $cnt++;
  30. }
  31. print "$cnt\n";
  32.  
Thanks
Kumar
Jan 8 '09 #5

KevinADC
Expert 2.5K+
P: 4,059
I don't know what you mean:

"How to insert the code to remove the duplicate entries of song in the same code."

Filenames in the same directory must be unique so there will be no duplicate filenames in "/home/kk/song/latest" after running the above code.

If you mean something else try and explain clearly what it is.
Jan 8 '09 #6

Post your reply

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