如何使用 Perl 在 Windows 上创建 Unicode 目录? [英] How do I create a Unicode directory on Windows using Perl?
问题描述
我努力创建包含 Unicode 的目录名称.我使用的是 Windows XP 和 Perl Camelbox 5.10.0.
I struggle to create directory names containing Unicode. I am on Windows XP and Perl Camelbox 5.10.0.
到目前为止,我曾经使用 File::Path qw ( make_path )
来创建目录 - 在第一个西里尔文目录出现之前它一直工作正常.
Up until now I used to use File::Path qw ( make_path )
to create directories - which worked fine until the first cyrillic directory appeared.
对于文件 Win32API::File qw ( CreateFileW )
如果文件名是 UTF-16LE 编码,则工作正常.目录有没有类似的东西?或者也许是一个参数来告诉 CreateFileW
如果它不存在则创建 Unicode 路径?
For files Win32API::File qw ( CreateFileW )
works fine if the file name is UTF-16LE encoded. Is there something similar for directories? Or maybe a parameter to tell CreateFileW
to create the Unicode path if it doesn't exist?
谢谢,
内尔
推荐答案
Win32.pm为 CreateDirectory
和朋友提供一个接口:
Win32.pm provides an interface to CreateDirectory
and friends:
Win32::CreateDirectory(DIRECTORY)
创建 DIRECTORY
并在成功时返回真值.在失败时检查 $^E
以获取扩展错误信息.
Creates the DIRECTORY
and returns a true value on success. Check $^E
on failure for extended error information.
DIRECTORY 可能包含系统代码页之外的 Unicode 字符.创建目录后,您可以使用 Win32::GetANSIPathName()
获取可以传递给系统调用和外部程序的名称.
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
.
Previous answer:
Note: Keeping this here for the record because you were trying to use CreateDirectoryW
directly in your program.
要手动执行此操作,请导入 CreateDirectoryW
使用 Win32::API:
To do this by hand, import CreateDirectoryW
using Win32::API:
Win32::API->Import(
Kernel32 => qq{BOOL CreateDirectoryW(LPWSTR lpPathNameW, VOID *p)}
);
您需要对 CreateDirectoryW
的 $path
进行编码:
You need to encode the $path
for CreateDirectoryW
:
#!/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 ");
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 москва
这篇关于如何使用 Perl 在 Windows 上创建 Unicode 目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!