Search This Blog

Tuesday, February 13, 2018

PowerShell -Compare drive Folder File and SharePoint Doc Folder File



if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$destination = "E:\JaisonArumugam\Library"
$webUrl = "http://testSite"
$listUrl = "http://testSite/PhotoGallery/"

$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)


$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "Report"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "$("SharePoint FileName") `t$("Folder FileName") `t$("SharePoint FilePath") `t$("Folder FilePath") `t$("Count")"
$header | out-file -FilePath $logfile


$localFolderPath = "E:\JaisonArumugam\Library\"
$count=0
$dirfiles = gci -Recurse $localFolderPath | select  name | sort parent
$fileCount=0
$str1 = ""
$str2 = ""
$listUrl="";
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
        $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()
      
        ForEach($dfile in $dirfiles)
            {  $str1 =$file.Name;
               $str2 =$dfile.Name;
                if($str1 -eq $str2){
                 $count++;
                 write-host "folder file : " $dfile.Name,"sharepoint file : " $file.Name,$count

                  $header = "$($file.Name) `t$($dfile.Name) `t$($web.Url+"/"+$listUrl) `t$($destinationfolder) `t$($count)"

                  $header | out-file -FilePath $logfile  -append
               }             
            }
        }      
}

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



No comments:

Post a Comment