返回列表 发帖

[原创教程] [总结]Perl在遇到Unicode字符文件名时的各种处理方法

本帖最后由 523066680 于 2015-3-27 15:28 编辑

环境 XP/WIN7  Perl v5.16
编辑整理:523066680

常见的那些文件操作函数都不支持,于是为了达到目的,需要各种方法配合,应该是不如其他语言方便。
我只是想看看Perl到底是否适合做这件事,于是折腾了一回。

文件的建立:

    模块:Win32
    use Win32;
    use utf8;
    use Encode;
    #接受unicode传参
    Win32::CreateFile("W32CreateFile・测试");COPY
    特性: 成功返回true,但不返回文件句柄
    Creates the FILE and returns a true value on success.
    Check $^E on failure for extended error information.

    模块:Win32API::File
    函数:$hObject= CreateFileW( $swPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel )
    $hObject可以返回文件对象句柄
    注意事项: 传入的文件路径的编码格式为:UTF16-LE ,必须以\x00结尾,示例(代码保存为utf8格式):
    use Win32API::File qw(:ALL);
    use utf8;
    use Encode;
    $str="文tes・t.txt\x00";
    $hobject=CreateFileW(encode('UTF16-LE', $str), GENERIC_WRITE, 0, [], OPEN_ALWAYS,0,0);COPY


目录的建立

    模块:Win32
    use Win32;
    use utf8;
    Win32::CreateDirectory("Dir・测试");COPY

文件的枚举

    在遇到unicode字符的时候,File::Find模块 以及 IO::Dir 模块都只能输出文件短名。
    暂时用CMD /U Dir 的方法输出文件列表(郁闷吧,暂时没找到能完美操作的内置模块)
    参考文章
    http://www.perlmonks.org/?node_id=536223
    how to read unicode filename


复制某个文件夹内的文件(文件名含unicode字符)

    模块:Win32API::File
    如果先获取文件的短名,然后再复制,但是目标文件名也会变成短名。
    于是暂时用cmd /U 模式获取文件列表,然后CopyFileW进行复制:
    use Win32API::File qw':ALL';
    use Encode;
    use utf8;
    my $src=encode('gbk','.\\测试目录');
    my $dst='.\\Target';
    #该目录只有一层,/s开关是为了列出完整的路径
    my $all=`cmd /U /C dir /s /b \"$src\"`;
    my $fn;
    foreach (split(/\x0d\x00\x0a\x00/, $all)) {
        $fn = encode('gbk', decode('utf16-le',$_))."\n";
        @xrr=split(/\x5c\x00/, $_);
        CopyFileW(
            $_ ."\x00",
            encode('utf-16le', decode('utf8', "$dst\\")).$xrr[$#xrr]."\x00",
            1
        );
        print "$^E\n" if ($^E);
    }
    <STDIN>;COPY
    细节一、
        正确地使用 split $all 截断utf-16le字符段落,分隔符为0d 00 0a 00
        参考枚举脚本

    细节二、
        如果用basename()分割路径,同样会遇到00被忽略的问题,'\\' 的U16LE
        编码是5C 00,但是basename 只按5C截断,剩下的00造成了处理乱码。

        测试basename的第二个参数设置为 "\x5c\x00" 并不能解决这个问题
        解决方法一、
            手工去掉开头处00
        方法二、
            先转为GBK,再获取basename,再转utf-16le
            2014-12-12 备注这种方法在LongPath的情况下,会丢失unicode字符
            可以考虑转为UTF-8,不管怎么说都有点绕
        方法三、
            自己用正则表达式获取
            /\x5C\x00([^\x5c]+)$/;
            $1
        方法四、
            @xrr=split(/\x5c\x00/, $_);
            $xrr[$#xrr]

    细节三、
        CopyFileW复制文件时,要在末尾加\x00作为字符串终止符
        否则各种问题=_=


判断文件是否存在:

    方法一:先转为短名再判断,不做赘述
    方法二:渣方法,用CreateFileW测试建立同名文件,看是否有冲突


重命名:

    模块:Win32API::File
    MoveFileW(
        encode('utf-16le', decode('utf8',$F))."\x00",
        encode('utf-16le', decode('utf8',$newname))."\x00"
        );COPY


获取文件的日期信息:

    普通文件名的情况
    http://stackoverflow.com/questions/1839877/
    how-can-i-get-a-files-modification-date-in-ddmmyy-format-in-perl

    含有Unicode字符的文件名的情况
    http://www.perlmonks.org/?node_id=741797
    How to stat a file with a Unicode (UTF16-LE) filename in Windows?
    其中的方法是通过createfileW 获取文件句柄,然后用OsFHandleOpen获取通用的文件句柄对象,并传入state
    (感觉特别绕)

    另一种就是先转为短名再获取日期,但是这种方法在处理文件量大的时候,效率非常低。前面perlmonks中的方法
    效率要高得多
    use utf8;
    use Encode;
    use Win32;
    $filename='D:\测试目录\董贞 ・ 01.剑如虹.[贞江湖].mp3';
    $filename=Win32::GetShortPathName($filename);
    my $mtime = (stat $filename)[9];
    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
    $year+=1900;
    $mon+=1;
    print "$year-$mon-$mday\n";
    <STDIN>;COPY
[url=][/url]

本帖最后由 523066680 于 2015-3-27 16:32 编辑

回复 2# CrLf


     然后用Perl撸了一个将备份目录和源目录之间差异的文件进行提取的脚本。
准备改撸C艹但是估计要好长时间摸索了
[url=][/url]

TOP

本帖最后由 523066680 于 2015-3-27 16:59 编辑

回复 4# CrLf


    文件,不是文本。
用Robocopy的做目录同步的时候,如果选择 /MIR模式,会删除、替换三种文件:较旧的文件,较新的文件,多出的文件
然后撸了一个Perl专门提取这三种文件,支持unicode字符的路径、文件名

之前看到论坛有人介绍 SyncToy,试了一下,可以保留同步过程中被删除的文件,但是被统一存放在垃圾桶里…… 并且不包含目录结构
[url=][/url]

TOP

回复 6# tigerpower


    感谢分享,省了很多繁琐。
[url=][/url]

TOP

本帖最后由 523066680 于 2017-3-12 11:38 编辑

tigerpower 曾经在这里回复推荐了  Win32::Unicode,然后他自己删了帖。

我以前执着于用自带的模块做文件系统的事情,现在想想真没必要,应该怎么方便怎么来。

这里重新补充

http://bbs.bathome.net/redirect. ... 89&fromuid=3337
use Win32::Unicode;
use utf8;
my $dirname="CreateDir・测试";
my $dirname_long="CreateDir・测试1/CreateDir・测试2/CreateDir・测试3";
my $dirname_new="CreateDir・测试・新";
my $filename="CreateFile・测试";
mkdirW $dirname;
chdirW $dirname;
mkpathW $dirname_long;
$fh = Win32::Unicode::File->new('>', $filename);
$fh->close;
chdirW $dirname_long;
touchW $filename.'1';
chdirW '../../../..';
cptreeW $dirname.'/',$dirname_new;COPY
[url=][/url]

TOP

本帖最后由 523066680 于 2019-2-23 18:07 编辑

Chilkat模块亦支持unicode字符路径,而且还挺好用(这个模块很早就知道了,官网示例很齐全,就是没怎么用过)

CkDirTree参考文档
dirTree_iterate示例

对官方示例稍作修改,通过 put_Utf8 打开编码开关:
use chilkat();
use File::Slurp;
$dirTree = chilkat::CkDirTree->new();
$dirTree->put_BaseDir("F:/temp/UPathTest");
$dirTree->put_Recurse(1);
$dirTree->put_Utf8(1);
$success = $dirTree->BeginIterate();
if ($success != 1) {
    print $dirTree->lastErrorText() . "\r\n";
    exit;
}
my $buff = "";
while ($dirTree->get_DoneIterating() != 1)
{
    $buff .= $dirTree->fullPath() . "\r\n";
    #  Advance to the next file or sub-directory in the tree traversal.
    $success = $dirTree->AdvancePosition();
    if ($success != 1) {
        if ($dirTree->get_DoneIterating() != 1) {
            print $dirTree->lastErrorText() . "\r\n";
            exit;
        }
    }
}
write_file("Files.txt", {"binmode"=>":raw"}, $buff );COPY
[url=][/url]

TOP

返回列表