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 = 141有多少爱可以重来 迪克牛仔.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.

> ===== script as follows =========
> #!/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 and 
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 = 'd:\\test' ;

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

http://perl-begin.org/tutorials/bad-elements/#calling-variables-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 @files = 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 (@files) {

Don't iterate with $_ for anything half-serious like that. Use "foreach my 
$filename (@filenames) {".

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

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

>     }
> }
> print @files;

You probably want print map { "$_\n" } @files;

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to