- 论坛徽章:
- 12
|
环境 XP/WIN7 Active Perl v5.16
编辑整理:523066680
以下主要讨论用 ActivePerl 自带的模块的处理方法,更省心的办法请参考2楼
由于常见的那些文件操作函数都不支持,为了达到目的,需要各种方法配合。
以下脚本代码均保存为 utf8 编码格式。
文件的建立
模块: WIN32
use Win32;
use utf8;
#接受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结尾。
示例代码:
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); |
目录的建立
模块:Win32
use Win32;
use utf8;
Win32::CreateDirectory("Dir・测试"); |
枚举文件复制某个文件夹内的文件
模块: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;
for (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);
} |
这里有几个注意事项细节一、
正确地使用 split $all 截断utf-16le字符段落,分隔符应为0d 00 0a 00
细节二、
如果用 basename() 分割路径,同样会遇到00被忽略的问题,'\\' 的U16LE
编码是5C 00,但是basename 只按5C截断,剩下的00造成了处理乱码。
测试basename的第二个参数设置为 "\x5c\x00" 并不能解决这个问题
解决方法
手工去掉开头处的 \x00
或者:
@xrr=split(/\x5c\x00/, $_);
细节三、
CopyFileW复制文件时,要在末尾加\x00作为字符串终止符,否则各种问题=_=
判断文件是否存在
方法一:先转为短名再判断,不做赘述
方法二:渣方法,用CreateFileW测试建立同名文件,看是否有冲突
重命名
模块:Win32API::File
MoveFileW(
encode('utf-16le', decode('utf8',$F))."\x00",
encode('utf-16le', decode('utf8',$newname))."\x00"
); |
获取文件的日期信息
普通文件名的情况含有Unicode字符的文件名的情况
[How to stat a file with a Unicode (UTF16-LE) filename in Windows?](fhttp://www.perlmonks.org/?node_id=741797)
其中的方法是通过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"; |
[Finished in 0.4s] |
Perl, Win32, Unicode, Perl, Win32, Unicode, Perl, Win32, Unicode, Perl, Win32, Unicode, Perl, Win32, Unicode, Perl, Win32, Unicode, Perl, Win32, U3809, Win32,
评分
-
查看全部评分
|