in reply to Re: Rename Windows files with Unicode chars
in thread Rename Windows files with Unicode chars

The line

rename qq{I-\x{2665}-Perl}, 'I-love-Perl';

is cheating. You were able to type in the name of the file that was to be renamed because you already knew what the original file name was. The problem is that I will never have such knowledge beforehand.

Can you show code that will read a file name in the given directory, complete with the file's non-ascii characters, save the original file name in some variable, then strip the non-ascii from the said file name, then rename the file (using the original, saved file name) to the new all-ascii file name?

  • Comment on Re^2: Rename Windows files with Unicode chars

Replies are listed 'Best First'.
Re^3: Rename Windows files with Unicode chars
by beech (Parson) on Aug 27, 2016 at 03:44 UTC

    Hi,

    Did you forget about  sub listDir { ? How does listDir cheat at reading unicode filenames?

    :)

      I needed to have the file name as read by listDir to be what is used during the renaming. To test if the concept worked I stripped the directory of all but your script. I then created two globals, $global_file_name and $new_name. Your code created I-unicode heart-Perl. When listDir read in the file name I saved it in $global_file_name. Just prior to the rename I saved $global_file_name into $new_name. Just before the rename line I did


      $new_name =~ s![^:ascii:]!!ig;

      I then changed the rename line to

      rename $global_file_name, $new_name;

      It worked. I now know I can use this concept to strip out the unicode from a large number of files. Thank you very much for your help