CkDirTree参考文档
dirTree_iterate示例
use chilkat();
$dirTree = chilkat::CkDirTree->new();
# Specify the root of the directory tree to be traversed.
$dirTree->put_BaseDir("c:/temp/abc123");
# Indicate that we want to recursively traverse the tree.
$dirTree->put_Recurse(1);
# Begin the directory tree traversal.
$success = $dirTree->BeginIterate();
if ($success != 1) {
print $dirTree->lastErrorText() . "\r\n";
exit;
}
while ($dirTree->get_DoneIterating() != 1) {
# Examine the file or directory at the current traversal position:
if ($dirTree->get_IsDirectory() == 1) {
print "(sub-directory)" . "\r\n";
}
else {
# Note: There is also a FileSize64 property...
print "Size in bytes: " . $dirTree->get_FileSize32() . "\r\n";
}
print "Relative Path: " . $dirTree->relativePath() . "\r\n";
print "Absolute Path: " . $dirTree->fullPath() . "\r\n";
print "Absolute UNC Path: " . $dirTree->fullUncPath() . "\r\n";
print "----" . "\r\n";
# Advance to the next file or sub-directory in the tree traversal.
$success = $dirTree->AdvancePosition();
if ($success != 1) {
if ($dirTree->get_DoneIterating() != 1) {
print $dirTree->lastErrorText() . "\r\n";
exit;
}
}
}
该模块支持Unicode路径名称,通过 put_Utf8 打开编码开关,将上面代码稍作修改:
use chilkat();
use File::Slurp;
$dirTree = chilkat::CkDirTree->new();
$dirTree->put_BaseDir("F:/temp/UPathTest");
$dirTree->put_Recurse(1);
$dirTree->put_Utf8(1);
$success = $dirTree->BeginIterate();
if ($success != 1) {
print $dirTree->lastErrorText() . "\r\n";
exit;
}
my $buff = "";
while ($dirTree->get_DoneIterating() != 1)
{
$buff .= $dirTree->fullPath() . "\r\n";
# Advance to the next file or sub-directory in the tree traversal.
$success = $dirTree->AdvancePosition();
if ($success != 1) {
if ($dirTree->get_DoneIterating() != 1) {
print $dirTree->lastErrorText() . "\r\n";
exit;
}
}
}
write_file("Files.txt", {"binmode"=>":raw"}, $buff );