Question

below is the code to iterate through root level folders which downloads folder starts with "a" and download sub folders from it starting with any letter. I want to download all the folder levels and not just the subfolder.how do I achieve this

foreach($folder in $RootFolder.SubFolders)
{
if($folder.Name.StartsWith("a")
{
ProcessFolder($folder.Url)
}
foreach($folder in $folders.SubFolders)
{
ProcessFolder($folder.Url)

}

}
Was it helpful?

Solution

I would do something like this:

function ProcessAllSubfolders($folderCollection)
{
    foreach ($folder in $folderCollection)
    {
        if ($folder.Subfolders.Count -gt 0)
        {
            ProcessAllSubfolders($folder.SubFolders)
        }

        ProcessFolder($folder.Url)
    }
}

# main

foreach ($folder in $RootFolder.SubFolders)
{
    if ($folder.Name.StartsWith("a"))
    {
        if ($folder.Subfolders.Count -gt 0)
        {
            ProcessAllSubfolders($folder.SubFolders)
        }

        ProcessFolder($folder.Url)
    }
}

(I realize there's some code duplication there and probably there's a more elegant way of doing it, but what do you want for a 3 minute answer?)

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top