Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: renaming filenames

by pvaldes (Chaplain)
on Aug 16, 2012 at 09:24 UTC ( #987715=note: print w/ replies, xml ) Need Help??


in reply to renaming filenames

ls * | perl -nle'$in=$_; s/canción/Poesía/; $out=$_; rename($in,$out)if!-e$out'

Works for me. Details in this node

Corrected -e$n: thanks to remiah


Comment on Re: renaming filenames
Download Code
Re^2: renaming filenames
by remiah (Hermit) on Aug 16, 2012 at 13:09 UTC
    Maybe trivial thing to say,
    rename($in,$out) if !-e $out
    doesn't it ?

      I'm using perl 5.12.4 on windows 7.
      Your code doesn't work in windows.

        This shouldn't be a problem for you, use a linux live-cd...

        If I have a file called canción, without any extension, and I want to rename it to poesía, without extension, it doesn't do it...

        It only works if the filename has at least 8 characters including point and extension. Moreover, if the $to filename, has equal or more number of characters than the $from filename and this has at least 10 characters, strange characters appear on the $to filename.

        Windows related problems I bet... avoid yourself a lot of trouble and try linux for this specific task

        Some questions.

        1. Active Perl or Strawberry Perl ?

        2. What shows your $_ in your program ? With my XP, it shows 8.3 style file names(For example, "Program Files"=> Progra~1).

        foreach (@files) { print "$_\n"; my $original = $_; s/canción/poesía/; rename "$directory\\$original", "$directory\\$_"; }
        What would $_ with Windows 7?.

        3. When you install Win32::Unicode,Win32::Unicode::Dir, Does this work ?

        use strict; use warnings; use utf8; use Win32::Unicode; use Win32::Unicode::Dir; my $wdir = Win32::Unicode::Dir->new; $wdir->open('./'); while ( my $file=$wdir->fetch ) { next if( $file !~ /^canción/ ); my $file_new=$file; $file_new =~ s/canción/poesía/; printW "$file ==> $file_new\n"; #moveW($file, $file_new) or die $!; } $wdir->close;
        I tried with ActivePerl. As "andalou" says, I also met the same error when calling moveW. It seems lacking XS's function which is calling Win32API. I guess Strawberry Perl works ... fine?

        Update:
        I understand OP="andalou" now. If you are using Active Perl,

        ppm install "http://sourceforge.jp/frs/g_redir.php?m=jaist&f=%2Fassp%2 +FASSP+V2+multithreading%2Fpackages%2FWin32-Unicode.ppd"
        will remove your Win32::Unicode error messages. </code>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://987715]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2015-07-05 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls