Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Wide characters in Windows filenames with File::Copy

by BillKSmith (Monsignor)
on Nov 21, 2023 at 00:43 UTC ( [id://11155728]=note: print w/replies, xml ) Need Help??


in reply to Wide characters in Windows filenames with File::Copy

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.

Bill

Replies are listed 'Best First'.
Re^2: Wide characters in Windows filenames with File::Copy
by ikegami (Patriarch) on Nov 21, 2023 at 03:26 UTC

    Not all systems use Windows-1252 as their ACP. You should be using "cp".Win32::GetACP() (and use Win32;) instead of "cp1252".

Re^2: Wide characters in Windows filenames with File::Copy
by slugger415 (Monk) on Nov 21, 2023 at 04:24 UTC

    thank you Bill!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11155728]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2025-07-13 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.