Perl rename function doesnt work!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tanichka
    New Member
    • Apr 2008
    • 1

    Perl rename function doesnt work!

    Hello you helpful folk! Please look at my script and tell me what I am doing wrong! I am cleaning the names of my music files from unnecessary symbols, and the cleaning works fine, however the perl rename function fails to do the actual renaming. I tried to rename a file in the same dir using strings instead of variables and that works!! But it seems I need to get it to work with vars as well. Can anyone give me a clue? Thanks!!
    [CODE=perl]
    $dirname = "C:/folder";

    opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
    while (defined($file = readdir(DIR))) {

    $neu = $file;
    print $neu;
    print "\n";

    $neu =~ tr/['&a-zA-Z]/ /c;
    print $neu;
    print "\n";

    $neu =~ tr/A-Z/a-z/;
    print $neu;
    print "\n";

    $neu =~ s/\b(\w+)\b/ucfirst($1)/ge;
    print $neu;
    print "\n";

    $neu =~ s/Mp/.mp3/;
    $neu =~ s/Wav/.wav/;
    $neu =~ s/Fl/.flv/;
    $neu =~ s/Vob/.vob/;
    $neu =~ s/'S/'s/g;

    print $neu;
    print "\n";

    $neu =~ tr/ //d;
    print $neu;
    print "\n";

    rename ("$file", "$neu");

    }

    #rename "C:/folder/03-kt_tunstall-one_day.mp3", "C:/folder/NEW NAME.mp3";

    closedir(DIR);[/CODE]
    Last edited by eWish; Apr 1 '08, 11:03 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    replace this line:

    rename ("$file", "$neu");

    with:

    print qq{rename ("$file", "$neu");\n} ;

    and see what the values of $file and $neu are. They are most likely just filenames with no directory path in them. You will probnably need to add the directory path.

    Comment

    • eggi
      New Member
      • Nov 2007
      • 9

      #3
      Hmm... that's odd.

      I created a file named "binger", ran the inside of your code and it did rename the file to "Binger."

      I would do as was suggested in the previous reply. It seems that the problem is most likely that the $file variable isn't getting set correctly or there are characters i teh name that arent' getting translated with "tr" properly. If you are using full path names, you may need to specifically account for the "/" characters.

      Hope that helps :)

      , Mike

      Code:
       #!/usr/bin/perl 
      $file = "binger";
      $neu = $file;
      print $neu;
      print "\n";
      $neu =~ tr/['&a-zA-Z]/ /c;
      print $neu;
      print "\n";
      $neu =~ tr/A-Z/a-z/;
      print $neu;
      print "\n";
      $neu =~ s/\b(\w+)\b/ucfirst($1)/ge;
      print $neu;
      print "\n";
      $neu =~ s/Mp/.mp3/;
      $neu =~ s/Wav/.wav/;
      $neu =~ s/Fl/.flv/;
      $neu =~ s/Vob/.vob/;
      $neu =~ s/'S/'s/g;
      print $neu;
      print "\n";
      $neu =~ tr/ //d;
      print $neu;
      print "\n";
      rename ("$file", "$neu");

      Comment

      Working...