Windows system Unicode File name operation (new, rename, enumerate, copy) all tips

Source: Internet
Author: User
Tags stdin

Common those file operation functions are not supported, so in order to achieve the goal, need a variety of methods to cooperate, should be less convenient than other languages.
I just want to see if Perl is the right thing to do, and then toss it back.

Creation of documents:

        module: Win32
Code:[Select all] [Expand/shrink] [Download] (example.pl)
    1. Use Win32;
    2. Use UTF8;
    3. Use Encode;
    4. #接受unicode传参
    5. Win32::CreateFile("W32createfile test");
        attribute: Successfully returns true but does not return a file
    handle

        creates the FILE and returns a true value on success.

        Check $^e On Failure for extended error information.



        module: Win32api::file

        function: $hObject = Createfilew ($swPath, $uAccess, $uShare, $pSecAttr, $uCreate
    , $uFlags, $hModel)

        $hObject can return a handle to a file object

    Precautions
        :

The encoded format of the incoming file path is: Utf16-le, which must end with \x00, example (the code is saved in UTF8 format):

Code:[Select all] [Expand/shrink] [Download] (example.pl)
  1. Use Win32API::File qw(: All);
  2. Use UTF8;
  3. Use Encode;
  4. $str="Wen tes?t.txt\x00";
  5. $hobject =createfilew (Encode Utf16-le" Span style= "color: #339933;" >, $str ) generic_ Write0 [] Open_always,< Span style= "color: #cc66cc;" >0,0;


Establishment of a directory

        module: Win32
Code:[Select all] [Expand/shrink] [Download] (example.pl)
    1. Use Win32;
    2. Use UTF8;
    3. Win32::createdirectory("Dir test");

Enumeration of files

        in the case of Unicode characters, the File::find module and IO::D IR module can only output short file names.

        temporarily use the cmd/u Dir method to output the file list (depressed bar, temporarily did not find the perfect operation of the built-in module)

        Reference Articles


http://www.perlmonks.org/?node_id=536223

    How to
        read Unicode filename



Copy a file within a folder (the file name contains Unicode characters)

        module: Win32api::file

        If you first get the short name of the file and then copy it, the target file name becomes a short name.

        then temporarily use cmd/u mode to get the file list, and then copyfilew to copy:
Code:[Select all] [Expand/shrink] [Download] (example.pl)
  1. Use Win32API:: "File qw': All ';
  2. Use Encode;
  3. Use UTF8;
  4. My $src=encode(' GBK ','. \ \ Test directory ');
  5. My $dst='. \\Target ';
  6. #该目录只有一层, the/s switch is to list the full path
  7. My $all=' cmd/u/C dir/s/b \ ' $src\ ';
  8. My $fn;
  9. foreach split (/\x0d\x00\x0a\x00/, $all ) {
  10.     $FN = Encode GBK ' decode< Span style= "color: #009900;" > ( ' Utf16-le ' ,$_) ) " \n "
  11. @xrr=split(/\x5c\x00/, $_);
  12. Copyfilew(
  13. $_ ." \x00 ",
  14. Encode(' Utf-16le ', decode(' UTF8 ', "$dst\ \")). $XRR[$ #xrr]." \x00 ",
  15. 1
  16. );
  17. print "$^e\ n" if ($^e);
  18. }
  19. <STDIN>;
        details One,

        Use split $all truncate the UTF-16LE character paragraph correctly, the delimiter is 0d 0a xx

        Reference Enumeration Script



        details two,

        If you use BaseName () to split the path, you will also encounter 00 ignored issues, ' \ \ ' U16le

        encoding is 5C 00, but basename only by 5C truncation, the remaining 00 caused the processing garbled.



    The
        second parameter of test basename is set to "\x5c\x00" and does not solve this problem

        workaround One,

        manually remove the starting point

        method Two,

        first turn to GBK, then get basename, then Utf-16le

        2014-12-12 Note This method loses Unicode characters in the case of Longpath

        you can consider turning into UTF-8, which is a little bit around anyway
    .

        method Three,

        get it yourself with regular expressions

        /\x5c\x00 ([^\x5c]+) $/;

      $

          method Four,

          @xrr =split (/\x5c\x00/, $_);

          $XRR [$ #xrr]



          details three,

          Copyfilew When copying a file, add \x00 as a string terminator at the end

          otherwise all sorts of problems =_=



    Determine if the file exists:

          method One: First to the short name and then judge, do not repeat

          method Two: Slag method, using Createfilew test to establish the same name file, see if there is a conflict



    Rename:

          module: Win32api::file
    Code:[Select all] [Expand/shrink] [Download] (example.pl)
    1. Movefilew(
    2.     Encode ( Decode ( " UTF8 ' , $F ) ) "\x00" ,
    3. Encode(' Utf-16le ', decode(' UTF8 ',$newname))." \x00 "
    4. );


    Get date information for a file:

          the case of a common file name


    http://stackoverflow.com/questions/1839877/

          How-can-i-get-a-files-modification-date-in-ddmmyy-format-in-perl



          case with a file name containing Unicode characters


    http://www.perlmonks.org/?node_id=741797

      How to
          stat a file with a Unicode (utf16-le) filename in Windows?

          the method is to get the file handle through Createfilew, and then use Osfhandleopen to get a generic file handle object and pass
      in the state

          (feel special around)



      The
          other is to go to the short name and then get the date, but this method is very inefficient when dealing with a large amount of files. Methods in the previous Perlmonks

          much higher efficiency.
    Code:[Select all] [Expand/shrink] [Download] (example.pl)
    1. Use UTF8;
    2. Use Encode;
    3. Use Win32;
    4. $filename=' d:\ Test catalog \ Dong Zhe? 01. The sword is like a rainbow. [Zhen hu Hu].mp3 ';
    5. $filename=win32::getshortpathname($filename);
    6. My $mtime = (stat $filename)[9];
    7. My ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $ISDST ) = localtime $mtime Span style= "color: #009900;" >)
    8. $year+ =1900;
    9. $mon+ =1;
    10. Print "$year-$mon-$mday\ n";
    11. <STDIN>;

    Windows system Unicode File name operation (new, rename, enumerate, copy) all tips

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.