Win32::File提供兩個函式來處理檔案的屬性:GetAtrributes和SetAttributes,分別包裝了Win32 API:GetFileAttributes和SetFileAttributes。
use Win32::File;
use strict;
my $file=shift || die "need a filename\n";
my $attribs=1;
Win32::File::GetAttributes($file, $attribs) or die "GetAttributes: $!";
print "ATTRIBS: ";
if($attribs & ARCHIVE){print "ARCHIVE ";}
if($attribs & READONLY){print "READONLY ";}
if($attribs & DIRECTORY){print "DIRECTORY ";}
print "\n";

GetAttributes不會在意你所傳給它的純量值。這裡指定它(my $attribs=1;)是為了防止編譯程式發出末初始化變數的警告。

SetAttributes函式要幅一個檔案名稱和一些指定給檔案的屬性。
use Win32::File;
use strict;
my $file=shift || die "need a filename\n";
my $attribs=1;
die "can't find $file:$!" unless -e $file;
Win32::File::GetAttributes($file, $attribs) or die "GetAttributes: $!";
$attribs |= READONLY;
Win32::File::SetAttributes($file, $attribs) or die "SetAttributes: $!";

上面的程式為檔案增加了READONLY的屬性
創作者介紹

神鵰蝦

jck11 發表在 痞客邦 PIXNET 留言(0) 人氣()