2

I have the PowerShell script which download Documents from SharePoint Document Library.i want to copy the modified date of documents including folders to the window explorer. Where I am going wrong here?

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://xyz:4444/Shared Documents")
#$site = "http://rssplsp08:9822"


function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
     $destinationfolder = $destination + "/" + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 

        }


    foreach ($file in $folder.Files) {
        #Ensure destination 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()
        $exportlist = @()
        $spQuery = New-Object Microsoft.SharePoint.SPQuery
        $spQuery.Folder=$folder;
        $items = $list.GetItems($spQuery);
        $items| foreach {

           $obj = New-Object PSObject -Property @{

                 "Modified Date" = $_["Modified"]


            }  

                        $a= Get-Item "$destinationfolder"
                        $a.LastWriteTime=$_["Modified"]
        }       



         }
        catch
        {
        #Add-content "c:\log3.txt"  $error
        Write "Error: $file.name: $_" >>c:\logfile.txt
        continue;
        }

        }
}

foreach($folder in $list.RootFolder.SubFolders)
{

if($folder.Name.StartsWith("T") )
{
    ProcessFolder($folder.Url)


       if ($folder.Subfolders.Count -gt 0)
        {
            ProcessAllSubfolders($folder.SubFolders)
        }

}
}

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

        }

        ProcessFolder($folder.Url)

    }
}
ProcessFolder($list.RootFolder.Url)
#Download root files

#Download files in folders

Your Answer

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

Browse other questions tagged or ask your own question.