[Perl] Windows 系统 Unicode 文件名操做(新建、重命名、枚举、复制)全攻略php
环境 XP/WIN7 Perl v5.16
编辑整理:PerlMonk、523066680node
常见的那些文件操做函数都不支持,因而为了达到目的,须要各类方法配合,应该是不如其余语言方便。
我只是想看看Perl究竟是否适合作这件事,因而折腾了一回。正则表达式
文件的创建:函数
模块:Win32 Code: [全选] [展开/收缩] [Download] (example.pl) use Win32; use utf8; use Encode; #接受unicode传参 Win32::CreateFile("W32CreateFile・测试"); 特性: 成功返回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格式): Code: [全选] [展开/收缩] [Download] (example.pl) 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);
目录的创建post
模块:Win32 Code: [全选] [展开/收缩] [Download] (example.pl) use Win32; use utf8; Win32::CreateDirectory("Dir・测试");
文件的枚举测试
在遇到unicode字符的时候,File::Find模块 以及 IO::Dir 模块都只能输出文件短名。 暂时用CMD /U Dir 的方法输出文件列表(郁闷吧,暂时没找到能完美操做的内置模块) 参考文章 http://www.perlmonks.org/?node_id=536223 how to read unicode filename
复制某个文件夹内的文件(文件名含unicode字符)ui
模块:Win32API::File 若是先获取文件的短名,而后再复制,可是目标文件名也会变成短名。 因而暂时用cmd /U 模式获取文件列表,而后CopyFileW进行复制: Code: [全选] [展开/收缩] [Download] (example.pl) 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>; 细节1、 正确地使用 split $all 截断utf-16le字符段落,分隔符为0d 00 0a 00 参考枚举脚本 细节2、 若是用basename()分割路径,一样会遇到00被忽略的问题,'\\' 的U16LE 编码是5C 00,可是basename 只按5C截断,剩下的00形成了处理乱码。 测试basename的第二个参数设置为 "\x5c\x00" 并不能解决这个问题 解决方法1、 手工去掉开头处00 方法2、 先转为GBK,再获取basename,再转utf-16le 2014-12-12 备注这种方法在LongPath的状况下,会丢失unicode字符 能够考虑转为UTF-8,无论怎么说都有点绕 方法3、 本身用正则表达式获取 /\x5C\x00([^\x5c]+)$/; $1 方法4、 @xrr=split(/\x5c\x00/, $_); $xrr[$#xrr] 细节3、 CopyFileW复制文件时,要在末尾加\x00做为字符串终止符 不然各类问题=_=
判断文件是否存在:编码
方法一:先转为短名再判断,不作赘述 方法二:渣方法,用CreateFileW测试创建同名文件,看是否有冲突
重命名:.net
模块:Win32API::File Code: [全选] [展开/收缩] [Download] (example.pl) MoveFileW( encode('utf-16le', decode('utf8',$F))."\x00", encode('utf-16le', decode('utf8',$newname))."\x00" );
获取文件的日期信息:code
普通文件名的状况 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中的方法 效率要高得多 Code: [全选] [展开/收缩] [Download] (example.pl) 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>;
=====================================================补充=========================================================
tigerpower 推荐了 Win32::Unicode
我之前执着于用自带的模块作文件系统的事情,如今想一想真不必,应该怎么方便怎么来。
这里从新补充
http://bbs.bathome.net/redirect.php?goto=findpost&ptid=34881&pid=168889&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;