The character sets of filesystems are one of the (few) blind spots in Perls support for character sets. Perl (and Unix, go figure) consider a filename to be a null-terminated sequence of bytes without further encoding and Perl normally uses the byte-oriented APIs. Windows likely treats the file as UTF-16 or as some other charset, so there will be an API mismatch when you input the filename to your Perl script and it then turns over that name to the byte-oriented APIs.

You can try to transcode the filename using Encode or you can resort to using the 8.3 filenames (if they are enabled for the filesystem/partition in question). You can also try to use the "wide" API (OpenFileW instead of the Perl default OpenFileA) which accepts UTF-16 strings (I believe). The drawback of the wide APIs is, that the rest of Perl, like filehandles, does not know what to do with the results you get back from them.

The most likely successful approach is to use pattern matching and readdir to find the files that interest you(r program). Hopefully the byte-oriented results of readdir() will still work when passed to open().

Update: cdarke points out that OpenFile* are for 16-bit compatibility only. CreateFile* are the way to do it under the newer versions. tye's and demerphq's Win32API::File provide the functions to use those calls and the functions even return Perl filehandles!


In reply to Re: Opening files with japanese/chinese chars in filename by Corion
in thread Opening files with japanese/chinese chars in filename by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.