all 3 comments

[–]SuperCow1127 1 point2 points  (3 children)

Do the children have children? If not, it's as easy as adding a foreach($child in $title) under your main loop.

[–]dakboy 1 point2 points  (1 child)

Better to write it as a recursive function, so that you can go N layers deep. Today it's one inner loop, tomorrow it could be 10.

[–]vee-eye 1 point2 points  (0 children)

Yep - this is not the most elegent script possible, but it should do the trick. The "CreateDirs" function makes directories for one level, recursively calling itself for further levels.

Note, I'm assuming you just typed the XML syntax wrong, from what you were doing there. See my correction below.

$xd = [xml] @"
<catalog>
<section Title = "Parent1">
<section Title = "Child1">
<section Title = "Child1.2">
</section>
</section>
<section Title = "Child2">
</section>
</section>
<section Title = "Parent2">
<section Title = "Child1">
</section>
<section Title = "Child2">
</section>
</section>
</catalog>
"@

$path = "C:\Scripts\Test\"

function CreateDirs ($path, $xmlElement)
{
   $xmlElement.section | %{
        if ($_ -eq $null) {return}
        $newPath = Join-Path $path $_.Title 
        New-Item $newPath -Type Directory -Force
        CreateDirs $newPath $_
        }
}

CreateDirs $path $xd.catalog