Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Renaming non-ascii files

by SolidState (Scribe)
on Jun 01, 2005 at 14:04 UTC ( [id://462460]=note: print w/replies, xml ) Need Help??


in reply to Renaming non-ascii files

Hi Jon12,
First of all, don't flame people that try to help you, or no one will want to help you. You got good advice about typos in your posted code. If the code you post is incorrect, it makes it harder for other people to help.

With that out of the way, I have to say I don't understand exactly the problems you are experiencing. Sometimes you refer to non-ascii files (contents are not ascii?) and sometimes to non-ascii filenames. So which is it?
I assume it is the filename, because that is what you seem to handle in your code. I'm not sure how to help, so I tried to simplify the code (so that it is easier to debug):

use strict; use warnings; my $count = 1; print "Path to directory for renaming:\n"; chomp(my $dirname = <STDIN>); chdir $dirname or die "Cannot change to $dirname"; opendir(DIR, $dirname) or die "Cannot open $dirname"; for my $myfile (grep {m/[^[:ascii:]]/} readdir(DIR)) { print "$myfile: Filename is non-ascii\n"; rename($myfile, "Non-Ascii_Filename_$count.txt") or warn $!; $count++; } closedir(DIR);

If this doesn't work, I'd like to hear how does a simple Perl "rename" of an ascii filename in the same directory and with the same permissions work.

If a simple ascii filename can be renamed without a problem, try doing "print for (readdir(DIR));" instead of the entire "for" loop in the above program. Look at how Perl treats the non-ascii filenames.

Other then that, I can only recommend trying out the File::Rename module from CPAN, psossibly it offers a more robust solution.

Good luck :)

Replies are listed 'Best First'.
Re^2: Renaming non-ascii files
by Anonymous Monk on Jun 03, 2005 at 11:47 UTC
    I see where you are going, but it will not work.

    The reason I did not use:

    m/[^:ascii:]]/
    is Perl does not see the non-ascii characters at all. It sees only question marks for filenames. Even though in Windows the filenames appear in a native lanuage like Japanese or Arabic. Example: ??? ?? ???.txt is what Perl sees as a non-ascii filename. Therefore, I picked up non-ascii files using:
    m/[\?]+/
    and it worked...sometimes. The reason it does not work all the time is that if a file has exactly the same number of question marks perl considers it a duplicate and will not rename it.

    I need a method to make Perl, when reading a directory or globbing(i've tried globbing many of times, no go) to see these filenames as they are, not as question marks.

    thank for your assistance

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2024-03-28 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found