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.