本文介绍WIN32下perl如何取得和修改文件的属性,供大家学习参考。
需要: use Win32::File;
然后使用如下方法:
参考perldoc,可供设置的属性如下:
为了方便理解,我在程序里把每个选项对应的数值都打印出来附在后面了,每个选项都是只占一位的,因此我们可以用或|来同时应用多个属性,如下:
Win32::File::SetAttributes($name, ARCHIVE|HIDDEN);
这样文件同时是隐藏和存档文件了。
use Win32::File;
Win32::File::SetAttributes($name, $attr);
#!/usr/bin/perl -w
use Win32::File;
$name = "yourFile.txt";
$attr = 0;
Win32::File::GetAttributes($name, $attr) or die "Can't get attributes for $name.";
print "File attributes: $attrn";
if ($attr & READONLY) {
print "$name is read only.n";
}
if ($attr & ARCHIVE) {
print "$name has ARCHIVE set.n";
}
if ($attr & HIDDEN) {
print "$name is hidden.n";
}
if ($attr & SYSTEM) {
print "$name has SYSTEM set.n";
}
if ($attr & COMPRESSED) {
print "$name has COMPRESSED set.n";
}
if ($attr & DIRECTORY) {
print "$name has DIRECTORY set.n";
}
if ($attr & NORMAL) {
print "$name has NORMAL set.n";
}
if ($attr & OFFLINE) {
print "$name has OFFLINE set.n";
}
if ($attr & TEMPORARY) {
print "$name has TEMPORARY set.n";
}