how to rename files that contain chinese characters

--0-1345919640-1303701774=:80862
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hi,=0AI am using windows operating system.=0AI wanted to rename some files =
within certain directories and my files contain =0Achinese characters. Afte=
r renaming, I could not see those chinese characters, =0Awhat must I do to =
retain those chinese characters. Below is the file name and =0Athe script. =
Thanks=0Afile name =3D 141=E6=9C=89=E5=A4=9A=E5=B0=91=E7=88=B1=E5=8F=AFä=
=BB=A5=E9=87=8D=E6=9D=A5 =E8=BF=AA=E5=85=8B=E7=89=9Bä=BB=94.mp3=0Aafter r=
enaming the file is testing123~1.MP3=0A=0A=3D=3D=3D=3D=3D script as follows=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A#!/usr/bin/perl=0Ause File::Copy;=0Amy $dire=
ctories =3D 'd:\\test' ;=0Aopendir (MYHANDLE , $directories) || die "Cant o=
pen directories :$! ";=0A=C2=A0 my [at] files =3D readdir MYHANDLE;=0Aclosedir =
MYHANDLE;=0Aforeach ( [at] files) {=0A=C2=A0=C2=A0=C2=A0 if (/(^141)(.+$)/){=0A=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 my $original =3D $_;=0A=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 s/(^141)(.+$)/testing123$2/;=0A=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 rename "$directories\\$original", "$directories\\$_";=0A=C2=
=A0=C2=A0=C2=A0 }=0A}=0Aprint [at] files;
--0-1345919640-1303701774=:80862--
eventual [ Mo, 25 April 2011 05:22 ] [ ID #2058738 ]

Re: how to rename files that contain chinese characters

--0-1522745954-1303721457=:46258
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Tks Brian,=0Awith the link you provide, I then change the control panel/reg=
ional and language =0Asettings/language for non-unicode programs to "chines=
e (simplified prc)" and now =0AI can change files with chinese characters.=
=0AThanks=0A=0A=0A=0A=0A________________________________=0AF rom: Brian Fras=
er <fraserbn [at] gmail.com>=0ATo: eventual <eventualdeath [at] yahoo.com>=0ASent: Mo=
n, April 25, 2011 11:38:17 AM=0ASubject: Re: how to rename files that conta=
in chinese characters=0A=0AOn Mon, Apr 25, 2011 at 12:22 AM, eventual <even=
tualdeath [at] yahoo.com> wrote:=0A=0AHi,=0A>I am using windows operating system=
..=0A>I wanted to rename some files within certain directories and my files =
contain=0A>chinese characters. After renaming, I could not see those chines=
e characters,=0A>what must I do to retain those chinese characters. Below i=
s the file name and=0A>the script. Thanks=0A>file name =3D 141=E6=9C=89=E5=
=A4=9A=E5=B0=91=E7=88=B1=E5=8F=AFä=BB=A5=E9=87=8D=E6=9D=A5 =E8=BF=AA=E5=
=85=8B=E7=89=9Bä=BB=94.mp3=0A>after renaming the file is testing123~1.MP3=
=0A>=0A>=3D=3D=3D=3D=3D script as follows =3D=3D=3D=3D=3D=3D=3D=3D=3D=0A>#!=
/usr/bin/perl=0A>use File::Copy;=0A>my $directories =3D 'd:\\test' ;=0A>ope=
ndir (MYHANDLE , $directories) || die "Cant open directories :$! ";=0A>=C2=
=A0 my [at] files =3D readdir MYHANDLE;=0A>closedir MYHANDLE;=0A>foreach ( [at] file=
s) {=0A>=C2=A0=C2=A0=C2=A0 if (/(^141)(.+$)/){=0A>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 my $original =3D $_;=0A>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 s=
/(^141)(.+$)/testing123$2/;=0A>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A 0=C2=A0 rename =
"$directories\\$original", "$directories\\$_";=0A>=C2=A0=C2=A0=C2=A0 }=0A>}=
=0A>print [at] files;=0A=0AHey. Unfortunately, besides printing, none of the bu=
ilt-in IO/system operations =0Acurrently work with Unicode. You'll have to =
do some extra work:=0A=0Ahttp://www.i-programmer.info/programming/other-la n=
guages/1973-unicode-issues-in-perl.html=0A=0A=0ABrian.
--0-1522745954-1303721457=:46258--
eventual [ Mo, 25 April 2011 10:50 ] [ ID #2058740 ]

Re: how to rename files that contain chinese characters

Hi eventual,

On Monday 25 Apr 2011 06:22:54 eventual wrote:
> Hi,
> I am using windows operating system.
> I wanted to rename some files within certain directories and my files
> contain chinese characters. After renaming, I could not see those chinese
> characters, what must I do to retain those chinese characters. Below is
> the file name and the script. Thanks
> file name =3D 141=E6=9C=89=E5=A4=9A=E5=B0=91=E7=88=B1=E5=8F=AFä=BB=A5=
=E9=87=8D=E6=9D=A5 =E8=BF=AA=E5=85=8B=E7=89=9Bä=BB=94.mp3
> after renaming the file is testing123~1.MP3
>

I don't have the right font (hoping this message was indeed Unicode, and
declared the encoding right), and I have not worked programaticaly with
Unicode filenames in Windows (being Linux-hosted, etc.). It seems that Perl=

has been gaining some popularity as a scripting and programming tool for
Windows systems, which sounds encouraging.

A few comments on your code though.

> =3D=3D=3D=3D=3D script as follows =3D=3D=3D=3D=3D=3D=3D=3D=3D
> #!/usr/bin/perl

Always add "use strict;" and "use warnings;". It will prevent many common
errors. (Many people believe that it's a step forward because then you have=
to
declare many variables, but it's a descent for the purpose of short-term an=
d
long-term ascent, and *is* necessary.)

Your editor should have a way to add it automatically for every file. I don=
't
know what it is, but you should use https://duckduckgo.com/ or
http://www.google.com/ for that. (It is believed that duckduckgo is better =
for
many software development-related searches and for Perl especially.).

> use File::Copy;

Nice!

> my $directories =3D 'd:\\test' ;

Is it "directories" or "directory"? Also see:

http://perl-begin.org/tutorials/bad-elements/#calling-variab les-file

It better be "dir_path" or "dir_pathname" or whatever.

> opendir (MYHANDLE , $directories) || die "Cant open directories :$! ";

You should avoid using global file handles, but it shouldn't matter here.

> my [at] files =3D readdir MYHANDLE;
> closedir MYHANDLE;

Nice idiomatic Perl but see File::Spec's no_upwards:

http://perldoc.perl.org/File/Spec.html

There are also some convenient abstractions over File::Spec and similar
modules that you may wish to use:

* http://perl-begin.org/uses/sys-admin/

> foreach ( [at] files) {

Don't iterate with $_ for anything half-serious like that. Use "foreach my=

$filename ( [at] filenames) {".

> if (/(^141)(.+$)/){
> my $original =3D $_;
> s/(^141)(.+$)/testing123$2/;
> rename "$directories\\$original", "$directories\\$_";

OK, you really should use File::Spec here.

> }
> }
> print [at] files;

You probably want print map { "$_\n" } [at] files;

Regards,

Shlomi Fish

=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld

English spelling aims to be consistent. Publicly and methodically.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mo, 25 April 2011 11:12 ] [ ID #2058742 ]
Perl » gmane.comp.lang.perl.beginners » how to rename files that contain chinese characters

Vorheriges Thema: Template::Simple .04 and File::Slurp .16 are on cpan
Nächstes Thema: Help with indentation