473,321 Members | 1,778 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,321 software developers and data experts.

Renaming files

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
5 1666
numberwhun
3,509 Expert Mod 2GB
@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
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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

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

Similar topics

0
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
16
by: dudufigueiredo | last post by:
I have one folder containing mp3 files, the folder is: C:\My Shared Folder\Rubber Soul And the files are: 01 drive my car.mp3 02 norwegian wood.mp3 03 you won't see me.mp3 04 nowhere man.mp3...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
3
by: Colin | last post by:
I have two files and I want to rename file B to file A without doing a system call - can I do this in C using file pointers ?
6
by: Pegboy | last post by:
I am trying to create a DOS utility that will extract data from a file and use it to form a new filename for that same file. I can successfully open the file, get the data I need and form the new...
1
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
1
by: farseer | last post by:
HI, i have created a DataSet using the DataSet Designer. The default name is DataSet1. I then renamed this to "MyDataSet" and proceed to add my TableAdapters using the Visual Designer. After i...
1
by: Jack Maxwell | last post by:
Hope someone can help here. My brother has asked me if I can assist in renaming about 1500 midi files which are associated with a programm called Cubase SE. With that software installed he can...
1
by: GeoDW | last post by:
Hell All, I have looked around and not found the solution I am looking for within the old threads. Here is my problem: I have a directory full of .img and .rrd files that have long filenames...
2
by: Alan Mailer | last post by:
I am relatively new to VB.net. I want to change the name of a Project and change the name of the files in the Project's various folders to reflect that new project name. Is there something I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.