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. -
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
-
my $cnt = 1;
-
my $folder = "/home/kk/song/latest";
-
my @files = `ls $folder/*.mp3 *.MP3`;
-
my @sorted = sort { lc($a) cmp lc($b) } @files;
-
-
foreach my $file(@sorted)
-
{
-
my $name = $file;
-
$name=~ tr/a-z/A-z/;
-
$name=~ s/\.MP3//g;
-
$name=~ s/\.mp3//g;
-
chomp $name;
-
$name=~ s/_/ /g;
-
$name=~ s/__/ /g;
-
$name=~ s/[0-9]//g;
-
$name=~ s/\s+/ /g;
-
$name=~ s/\(.*\)//g;
-
# print "$name\n";
-
-
my $newname = $name.".MP3";
-
system 'mv',"$file", "/home/kk/song/latest/$newname";
-
-
# print "$newname\n";
-
$cnt++;
-
}
-
print "$cnt\n";
-
-
I also want to remove the duplicate songs but couldn't figure out how to apply it??
Thanks for the help
Kumar
5 1666 @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
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
Untested code, Try on a test driectory of files before using on your important files. Ask questions if necessary. -
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
-
my $cnt = 1;
-
my $folder = "/home/kk/song/latest";
-
chdir($folder) or die "$!";
-
opendir(DIR , '.') or die "$!";
-
my @files = grep{/\.mp3$/i} readdir DIR;
-
closedir DIR;
-
chomp(@files);
-
foreach my $old (@files){
-
my $new = $old;
-
$new =~ tr/0-9//d;
-
$new =~ s/_+/ /g;
-
$new =~ s/\s+/ /g;
-
$new =~ s/\(.*\)//g;
-
$new = uc($new);
-
# print "$new\n";
-
rename($old,$new) or die "$!";
-
# print "$new\n";
-
$cnt++;
-
}
-
print "$cnt\n";
-
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. -
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
-
my $cnt = 1;
-
my $folder = "/home/kk/song/latest";
-
chdir($folder) or die "$!";
-
opendir(DIR , '.') or die "$!";
-
my @files = grep{/\.mp3$/i} readdir DIR;
-
closedir DIR;
-
-
chomp(@files);
-
-
foreach my $old (@files){
-
my $new = $old;
-
$new =~ s/\.mp3//g;
-
$new =~ tr/0-9//d;
-
$new =~ s/\s-\s//g;
-
$new =~ s/^\s+//g;
-
$new =~ s/_+/ /g;
-
$new =~ s/\s+/ /g;
-
$new =~ s/\(.*\)//g;
-
$new =~ s/!+\w+//g;
-
$new = uc($new);
-
$new = $new.".MP3";
-
# print "$new\n";
-
rename($old,$new) or die "$!";
-
print "$new\n";
-
$cnt++;
-
}
-
print "$cnt\n";
-
Thanks
Kumar
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 ?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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...
|
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...
| |