On Thu, Jul 9, 2015 at 6:01 AM, Nagy Tamas (TVI-GmbH) <
[email protected]> wrote:
> Hi,
>
>
>
> The following code doesn’t recognize dirs. As I list the dir into the XML,
> it shows dirs as ordinary files.
>
>
>
> Like the –d would not work. If I add an extra branch to recognize files
> with –f, it doesn’t print either files at all nor dirs.
>
>
>
> sub Traverse
>
> {
>
> opendir(DIR, $dir) or die "Cannot open directory $dir:
> $!\n";
>
> my @files = readdir(DIR);
>
> closedir(DIR);
>
> foreach my $file (@files) {
>
> # generate XML here
>
>
>
> next if (($file eq '.') || ($file eq '..'));
>
>
>
> print $file;
>
> if((-d $file) and ($file !~ /^\.\.?$/) and
> ($file ne ".") and ($file ne "..")) {
>
> # make dir branch
>
> $writer->startTag("Folder",
> "Name" => $file);
>
> Traverse($file);
>
> $writer->endTag("Folder");
>
> } else {
>
> $writer->emptyTag("Object",
> "Name" => $file);
>
> # make file branch
>
> }
>
> }
>
> }
>
>
>
>
> Tamas
>
>
>
>From the readdir documentation:
If you're planning to filetest the return values out of a
"readdir", you'd better prepend the directory in question.
Otherwise, because we didn't "chdir" there, it would have been
testing the wrong file.
opendir(my $dh, $some_dir) || die "can't opendir $some_dir:
$!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
closedir $dh;
HTH,
Ken