The following code assumes that you want to create the file name encoded in a way that you can read it with the windows
dir command.
- Create a temp 'oldfile' in current directory.
- Copy it to a 'newfile'
- Delete the 'oldfile'.
- Search the directory for 'newfile'
- Verify that the 'newfile' name is recovered
In order to do this, it is necessary to encode
the name of 'newfile' for windows before the copy.
And to decode the name after ir is read back in. Run the windows
dir command to verify that the file name appears as you intend.
use strict;
use warnings;
use autodie;
use File::Copy;
use utf8;
use Encode qw(encode decode);
use Test::More tests=>1;
use File::Temp;
use Win32;
my $cp = "cp".Win32::GetACP(); # Update
my $oldname = tmpnam();
open my $make, '>', $oldname;
print $make "Any old thing\n";
close $make;
#my $newname = encode('cp1252', "Hildur_Guđnadóttir.txt");
my $newname = encode($cp, "Hildur_Guđnadóttir.txt");
copy($oldname, $newname);
unlink $oldname;
opendir my $dh, '.';
my $readbackname;
while (1) {
$readbackname = readdir $dh;
die "File not found\n" if !defined($readbackname);
last if $readbackname =~ m/^Hildur_Gu.nad.+ttir\.+txt/;
}
#$readbackname = decode('cp1252', $readbackname);
$readbackname = decode($cp, $readbackname);
is($readbackname, $newname, 'round trip');
UPDATE: Corrected code per ikegami's comment Re^2: Wide characters in Windows filenames with File::Copy below.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.