Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have the following script to download documents from document library. Is there any way I can download folders starting with A to J from root level which should include all of their sub folders from A to Z. Only from top level it should download A to j

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
    {
        Add-PSSnapin Microsoft.SharePoint.PowerShell
    }


    $destination = "C:\\Test\\"
    $web = Get-SPWeb -Identity "http://xyz:4444/"
    $list = $web.GetList("http://xyx:4444/Shared Documents/")


    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
            $destinationfolder = $destination + "/" + $folder.Url 
            if (!(Test-Path -path $destinationfolder))
            {
                $dest = New-Item $destinationfolder -type directory 
            }
            #Download file
            try
            {

            $binary = $file.OpenBinary()
            $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
            $writer = New-Object System.IO.BinaryWriter($stream)
            $writer.write($binary)
            $writer.Close()
             }
            catch
            {
            #Add-content "c:\log3.txt"  $error
            Write "Error: $file.name: $_" >>c:\logfile.txt
            continue;
            }

            }
    }

    $exportlist = @()
    $list.Items | foreach {
    $obj = New-Object PSObject -Property @{
    "Title" = $_["Title"]
    "Name" = $_["Name"]
    "Modified Date" = $_["Modified"]
    "Modified By" =$_["Modified By"]
    "Size"= $_["File Size"]
    "Path" = $web.Url + "/" + $_.File.Url

    }
    $exportlist += $obj
    $exportlist | Export-Csv -path 'C:\Test\MyList.csv' -noType
    }


    #Download root files
    ProcessFolder($list.RootFolder.Url)
    #Download files in folders
    foreach ($folder in $list.Folders) {
        ProcessFolder($folder.Url)

    }
share|improve this question
    
so what is exactly your problem? Is it the downloading of the structure of filtering it to A-J ? –  luccio Jun 25 at 10:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.