Renaming files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    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.
    Code:
    #!/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
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by kumarboston
    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.
    Code:
    #!/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
    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

    Comment

    • kumarboston
      New Member
      • Sep 2007
      • 55

      #3
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Untested code, Try on a test driectory of files before using on your important files. Ask questions if necessary.

        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 =~ 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";

        Comment

        • kumarboston
          New Member
          • Sep 2007
          • 55

          #5
          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.
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            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.

            Comment

            Working...