我很难创建包含Unicode的目录名称.我在Windows XP和Perl CamelBox 5.10.0上.
到目前为止,我曾经使用File :: Path qw(make_path)创建目录 – 在第一个西里尔语目录出现之前一直运行正常.
对于文件Win32API :: File qw(CreateFileW)如果文件名是UTF-16LE编码,则工作正常.目录有类似的东西吗?或者也许是一个参数,告诉CreateFileW创建Unicode路径,如果它不存在?
谢谢,NELE
解决方法
Win32.pm为CreateDirectory和朋友提供了一个界面:
Win32::CreateDirectory(DIRECTORY)
Creates the
DIRECTORY
and returns a true value on success. Check$^E
on failure for extended error information.DIRECTORY may contain Unicode characters outside the system codepage. Once the directory has been created you can use
Win32::GetANSIPathName()
to get a name that can be passed to system calls and external programs.
上一个答案:
注意:将此保留在此处作为记录,因为您尝试直接在程序中使用CreateDirectoryW.
要手动执行此操作,请使用Win32::API导入CreateDirectoryW
:
Win32::API->Import( Kernel32 => qq{BOOL CreateDirectoryW(LPWSTR lpPathNameW,VOID *p)} );
您需要为CreateDirectoryW编码$path:
#!/usr/bin/perl use strict; use warnings; use utf8; use Encode qw( encode ); use Win32::API; Win32::API->Import( Kernel32 => qq{BOOL CreateDirectoryW(LPWSTR lpPathNameW,VOID *p)} ); binmode STDOUT,':utf8'; binmode STDERR,':utf8'; my $dir_name = 'Волгогра́д'; my $ucs_path = encode('UCS-2le',"$dir_name\0"); CreateDirectoryW($ucs_path,undef) or die "Failed to create directory: '$dir_name': $^E";
E:\> dir 2010/02/02 01:05 PM волгогра́д 2010/02/02 01:04 PM москва