views:

269

answers:

1

I struggle to create directory names containing Unicode. I am on Windows XP and Perl Camelbox 5.10.0.

Up until now I used to use File::Path qw ( make_path ) to create directories - which worked fine until the first cyrillic directory appeared.

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?

Thanks,
Nele

+3  A: 

Win32.pm provides an interface to CreateDirectory and friends:

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.

Previous answer:

Note: Keeping this here for the record because you were trying to use CreateDirectoryW directly in your program.

To do this by hand, import CreateDirectoryW using Win32::API:

Win32::API->Import(
    Kernel32 => qq{BOOL CreateDirectoryW(LPWSTR lpPathNameW, VOID *p)}
);

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\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              москва
Sinan Ünür
@Sinan: This works perfectly. Thank you very much! :-)
Nele Kosog
Thank you. I'll use Win32::CreateDirectory. I am new to the Unicode universe and find it quite complicated - maybe this is only true on Windows using Perl - I don't know.
Nele Kosog