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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|