4

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)

}

}
3

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?)

  • it will download all folder levels from subfolders? – SharePointMan Mar 10 '14 at 16:13
  • Well, I haven't tested it, but the idea is that, for every folder, if it has subfolders, it tries to ProcessAllSubfolders. Then, for each of those subfolders, if they have subfolders, it tries to ProcessAllSubfolders, etc., etc. So theoretically, it should recursively go down through all the subfolders until it reaches folders that have no subfolders. Keep in mind, this is still starting only from folders at the root level who's name starts with "a". – Dylan Cristy Mar 10 '14 at 16:18
  • your logic seems to be correct.will test it tomorrow.i hope it will resolve my problem.i want my rootlevel folders to be searched with name a..but it should not affect all its subfolder levels – SharePointMan Mar 10 '14 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.