http://qs1969.pair.com?node_id=462384

jon12 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get some clarity on Perl's ability to rename non-ascii files. I would like to rename only non-ascii files in a directory that contains ascii and non-ascii filenames.

However, I am finding that with the standard Perl rename(oldfilename, newfilename) I recieve a "Permission Denied at <STDIN> line xxx" where xxx is the line containing the rename command. This error only appears when Perl attempts to rename a non-ascii filename.

Using the bactick rename: `rename \"oldfilename\" \"newfilename\"` the program seems to work with the caveat that Perl sees the non-ascii filenames as question marks ????? ????? and seems to find multiple duplicates and cannot rename all found. Error: Duplicate filename or file cannot be found.

Here is my code, I have many versions but this is my latest:

my $count = 1; print "Path to directory for renaming:\n"; chomp($dirname = <STDIN>); chdir $dirname or die "Cannot change to $dirname"; opendir(DIR, $dirname) or die "Cannot open $dirname"; while($myfile = readdir(DIR)) { $locate = telldir(DIR); if($myfile !~ m/[\?]+/gi) { print "$myfile: File is ascii\n"; } elsif($myfile =~ m/[\? ]+(\([0-9]+\))?\.(\w+)\i) #catches filenames + ???.txt or ????(2).txt { $count++; print "$myfile: File is non-ascii\n"; $ext = $2; $temp = "Non-Ascii_Filename_$count"; $newname = join "." $temp, $ext; rename($myfile, $newname) or warn $!; #rename \"$myfile\" \"$newname\"; #attempt with backtics } seekdir(DIR, $locate); } closedir(DIR);
Build: Active Perl v5.8.4
System: Windows XP Pro
Is there a better way to achieve the renaming of files, as neither of these methods are effective?
Thanks in advance.
john