Hi, That works fine, thx:
sub Traverse { find({wanted => sub { if(-d $File::Find::name) { $writer->startTag("Folder", "Name" => $File::Find::name); } }, postprocess => sub { $writer->endTag("Folder"); } }, "c:\\ph\\" ); } -----Ursprüngliche Nachricht----- Von: Nagy Tamas (TVI-GmbH) Gesendet: Freitag, 10. Juli 2015 09:16 An: $Bill; beginners@perl.org Betreff: AW: Traversing directory recursively Hi, I also tried the $writer = shift, but it drops an error for XML syntax. This version traverses only into one directory branch. So it opens a dir and goes down, list Everything but forgets to go down into the second or third dir. What does shift do actually? It tells that it is about array, but there is no array here. http://perldoc.perl.org/functions/shift.html sub Traverse { $dir = shift; if( opendir(DIR, $dir) ) { my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { # generate XML here $actualdir = $dir . "\\" ; next if (($file eq '.') || ($file eq '..')); print $file; if((-d $actualdir.$file) and ($file !~ /^\.\.?$/)) { # make dir branch $newpath = $actualdir.$file; $writer->startTag("Folder", "Name" => $newpath); Traverse($newpath, $writer); $writer->endTag("Folder"); } else { # make file branch $writer->emptyTag("Object", "Name" => $actualdir.$file); } } } } <ProjectStructure> <Folder Name="C:\ph\Oaktree"> <Folder Name="C:\ph\Oaktree\LeavesDir"></Folder> <Object Name="C:\ph\Oaktree\LeavesDir\FirstFile" /> <Object Name="C:\ph\Oaktree\LeavesDir\SecondFile" /> <Object Name="C:\ph\Oaktree\LeavesDir\ThirdFile" /> <Object Name="C:\ph\Oaktree\LeavesDir\FourthFile" /> </Folder> # these 3 are totally wrong: <Object Name="C:\ph\Oaktree\LeavesDir\RootDir1" /> <Object Name="C:\ph\Oaktree\LeavesDir\RootDir2" /> <Object Name="C:\ph\Oaktree\LeavesDir\RootDir3" /> </ProjectStructure> Dir structure: C:\ph\Oaktree\LeavesDir C:\ph\Oaktree\LeavesDir\FirstFile C:\ph\Oaktree\LeavesDir\SecondFile C:\ph\Oaktree\LeavesDir\ThirdFile C:\ph\Oaktree\LeavesDir\FourthFile C:\ph\RootDir1 C:\ph\RootDir2 C:\ph\RootDir3 The last 3 are wrong in the XML file -----Ursprüngliche Nachricht----- Von: $Bill [mailto:dbec...@gmail.com] Gesendet: Freitag, 10. Juli 2015 07:55 An: Nagy Tamas (TVI-GmbH) Betreff: Re: Traversing directory recursively use strict; use warnings; use IO::File; use XML::Writer; my $output = IO::File->new(">default.xml"); my $writer = XML::Writer->new(OUTPUT => $output, DATA_MODE => 1, DATA_INDENT => " ", ENCODING => "utf-8", NEWLINES => 0 ); $writer->startTag("ProjectStructure"); Traverse ('C:/ph', $writer); # added writer arg $writer->endTag("ProjectStructure"); #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - sub Traverse { my $dir = shift; my $writer = shift; opendir (DIR, $dir) or die "Cannot open directory $dir: $!\n"; my @files = readdir DIR; closedir DIR; foreach my $file (sort @files) { if (-d $file) { next if $file =~ /^\.\.?$/; # make dir branch $writer->startTag("Folder", "Name" => $file); Traverse("$dir/$file", $writer); $writer->endTag("Folder"); } else { # make file branch $writer->emptyTag("Object", "Name" => $file); } } } __END__ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/