如何使用Perl在Windows上创建Unicode目录? [英] How do I create a Unicode directory on Windows using Perl?

查看:93
本文介绍了如何使用Perl在Windows上创建Unicode目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难创建包含 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) code>创建目录 - 在第一个西里尔文件目录出现之前工作正常。

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?

谢谢, br /> Nele

Thanks,
Nele

推荐答案

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)}
);

您需要将 $ path 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\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              москва

这篇关于如何使用Perl在Windows上创建Unicode目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆