Wasnt sure where to post this so here it is!

Discussion in 'Site & Server Administration' started by Darkhodge, Jun 30, 2006.

  1. #1
    Hey,


    I have files on my site that have an ' in the filename which I need to remove. What would be the best method of removing these automatically?

    For exampe:

    Gun N' Roses - Sweet Child 'o Mine.gp3

    Needs to have all the ' removed...

    I cant do this manually as there are well over a 1000 files that need to be renamed... :(


    Anyway thanks in advance for any input :D
     
    Darkhodge, Jun 30, 2006 IP
  2. sreyas

    sreyas Well-Known Member

    Messages:
    128
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    Am not an expert in perl scripting but managed to write one for you. Note sure you already got it or not , anyway here is the code
    
    #!/usr/bin/perl
    use strict;
    use warnings;
    sub rnfile{
            my $new;
            $new = "@_";
            my $old=$new;
            print "Old Name :$new\n";
            $new =~s/'//g;
            print "New Name : $new\n";
            if($old ne  $new){
            system "mv \"$old\" \"$new\"";
            }
    }
    sub rndir{
            my @file =glob "*";
            foreach (@file)
                    {
                            rnfile $_;
                    }
    }
    rndir;
    
    
    Code (markup):
    Please save it as .pl file and execute using perl , please be in the folder where all the files are located.

    Thanks,
    Ciril
     
    sreyas, Jul 9, 2006 IP
    Darkhodge likes this.
  3. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #3
    Woooo - thank you!

    Ive never used perl so I'll give it a go!

    Thanks again. :D
     
    Darkhodge, Jul 10, 2006 IP
  4. wheel

    wheel Peon

    Messages:
    477
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You might be able to do it right from the command line if all the files are in the same directory:

    perl -i -e -p 's/\'//g' *

    should do it. Caveat emptor.
     
    wheel, Jul 10, 2006 IP
  5. wheel

    wheel Peon

    Messages:
    477
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry, just reread your post and you're renaming files - so my command won't do that. The above command replaces text inside files.
     
    wheel, Jul 10, 2006 IP
  6. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #6
    Does the code sreyas posted change just the filenames or the file contents as well? :eek:
     
    Darkhodge, Jul 10, 2006 IP
  7. sreyas

    sreyas Well-Known Member

    Messages:
    128
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Hey

    Actually my code was for renaming the files itself. Give it a try.

    Regards
    Ciril
     
    sreyas, Jul 10, 2006 IP
  8. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #8
    Ah rite awesome :D

    Just a question though. Basically the files Im renaming are sheet-music for the guitar. The file structure is as follows:

    If I place the perl script in "guitar-tabs" and run it, will it change all the file name under it including the subfolders? Or do I have to place the perl script in each and every one of the folders and run them?

    Thanks :)
     
    Darkhodge, Jul 10, 2006 IP
  9. sreyas

    sreyas Well-Known Member

    Messages:
    128
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Let me try for that one[;)]
     
    sreyas, Jul 10, 2006 IP
  10. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #10
    Huh? Sorry I didnt quite understand your last post! :eek:
     
    Darkhodge, Jul 10, 2006 IP
  11. sreyas

    sreyas Well-Known Member

    Messages:
    128
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #11
    Ok I managed to change that

    
    #!/usr/bin/perl
    use strict;
    use warnings;
    # Script to Remove ' from the file names
    sub rndir;
    sub rnfile{
            my $new;
            $new = "@_";
            my $old=$new;
            print "Old Name :$new\n";
            $new =~s/'//g;
            print "New Name : $new\n";
            if($old ne  $new){
            system "mv \"$old\" \"$new\"";
            }
    }
    sub printdir{
            foreach (@_){
                    if (-d $_){
                            rndir $_;
                              }
                    else
                            {
                            rnfile $_;
                            }
                    }
    }
    
    sub rndir{
            my @file =glob "@_/*";
            foreach (@file)
                    {
                            printdir $_;
                    }
    }
    rndir @ARGV;
    
    
    Code (markup):
    Regards
     
    sreyas, Jul 10, 2006 IP
  12. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #12
    Wow - thanks again! :D:D:D
     
    Darkhodge, Jul 10, 2006 IP
  13. sreyas

    sreyas Well-Known Member

    Messages:
    128
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #13
    Forgot to update run the script with directory name as input.
    for example: perl remove.pl song
     
    sreyas, Jul 10, 2006 IP
  14. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #14
    So with the file structure I mentioned above do I place it in the root folder and run it with "perl remove.pl guitar-tabs"?
     
    Darkhodge, Jul 10, 2006 IP
  15. sreyas

    sreyas Well-Known Member

    Messages:
    128
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #15
    Yup, perl remove.pl guitar-tabs
    I had trial run it many times. But you may first give it trial run aswell.
     
    sreyas, Jul 10, 2006 IP
  16. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #16
    Ok thanks again for all your help - youve been very helpful! :)
     
    Darkhodge, Jul 11, 2006 IP