本帖最后由 523066680 于 2015-3-27 15:28 编辑
环境 XP/WIN7 Perl v5.16
编辑整理:523066680
常见的那些文件操作函数都不支持,于是为了达到目的,需要各种方法配合,应该是不如其他语言方便。
我只是想看看Perl到底是否适合做这件事,于是折腾了一回。
文件的建立:
| use Win32; | | use utf8; | | use Encode; | | | | | | 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 |
目录的建立
| use Win32; | | use utf8; | | | | Win32::CreateDirectory("Dir・测试");COPY |
文件的枚举
复制某个文件夹内的文件(文件名含unicode字符)
模块:Win32API::File
如果先获取文件的短名,然后再复制,但是目标文件名也会变成短名。
于是暂时用cmd /U 模式获取文件列表,然后CopyFileW进行复制:
| use Win32API::File qw':ALL'; | | use Encode; | | use utf8; | | | | my $src=encode('gbk','.\\测试目录'); | | my $dst='.\\Target'; | | | | | | 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测试建立同名文件,看是否有冲突
重命名:
| MoveFileW( | | encode('utf-16le', decode('utf8',$F))."\x00", | | encode('utf-16le', decode('utf8',$newname))."\x00" | | );COPY |
获取文件的日期信息:
| 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 |
|