I get files in Windows 7 that have wide chars. I need to rename them after stripping the non-ascii characters from them. Opendir and then readdir (in a while clause) does not work because readdir does bytes!
Below is code that I also tried.
# File name = a1.pl
# Script to get rid of wide chars and non-ascii chars
# in a Windows 7 file name.
# This does not work.
# In the Windows 7 file explorer, the file name shows as
# "z ‎ay‎ ‎Pow‎.mp4"
# Note: There is at least one embedded wide char in the above
# pasted file name.
# The Windows 7 command promt shows the file name
# as
# "z ?ay? ?Pow?.mp4".
use 5.14.2;
# From: how to read unicode filename
# http://www.perlmonks.org/?node_id=536223
open fList, '-|:encoding(UTF-16LE)', 'cmd /U /C dir /W';
# Note: I tried to opendir and readdir. I got the shortened
# 8.3 character file name whenever a wide character
# was in the file name. I could not rename.
foreach (<fList>) {
utf8::encode($_);
my $orig_name = $_;
my $new_name = $_;
if ($new_name =~ m/.mp4/i) {
print " 1 orig_name is \"$orig_name\"\n";
$new_name =~ s![^[:ascii:]]!!ig;
print " 2 new name is \"$new_name\"\n";
rename "$orig_name", "$new_name"; # Does not work
}
}
__END__
In the results below, note that the end double quotes are not
at the end of the file name line! That should not be!
>a1.pl
1 orig_name is "z ΓÇÄayΓÇÄ ΓÇÄPowΓÇÄ.mp4
"
2 new name is "z ay Pow.mp4
"
-
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.
|