Friday, 4 July 2014

PowerShell - Script used to get list of files with same name in document library

PowerShell - Script used to get list of files with same name in document library:-
Input:-
1. Pass site URL and List name as input to the function to generate log with list of files with same name in that library.

Code:-
$LogFilePath =  Read-Host "Enter Log file path (C:/...../Log.txt)"

# =========================== LOADING SHAREPOITN POWERSHELL SNAPIN FILE DETAILS =====================================
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
                Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
                $snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"
 
                Write-Host $snapin

}

function Get-SPFilesInList
{
    param ($Url, $ListName)
 
    $web = Get-SPWeb -Identity $Url
    $list = $web.Lists.TryGetList($ListName)
    $folderExists=$false;  

    if ($list -ne $null)
    {
        #write-host $ListName $list.Items.count;
        $ItemsColl = $list.Items
        foreach ($item in $ItemsColl)
        {
            $documentType='';
    $docTypeWIthValues= $item["Debt Document Type"]
    if($docTypeWIthValues -ne $null)
    {
    $doctypeParts=$docTypeWIthValues.toString().split('|');
    $documentType= $doctypeParts[0];
    }
            #write-host $documentType
          [string]$fileNameWithExt=$item.File.Name;
          [string]$fileName = [System.IO.Path]::GetFileNameWithoutExtension($item.File.Name);
          #[string]$fileName = $item.File.Name;
          #write-host $fileName
          [int]$sameFileNameCount = 0        
          foreach ($itemToCompare in $ItemsColl)
            {
                [string]$fileNameToCompare = [System.IO.Path]::GetFileNameWithoutExtension($itemToCompare.File.Name);
                #[string]$fileNameToCompare = $itemToCompare.File.Name;
                if ($fileName -eq $fileNameToCompare)              
                {
                    $sameFileNameCount=$sameFileNameCount+1
                }
            }          
            if ($sameFileNameCount -gt 1)
            {
                #write-host $item.File.Name "- " $sameFileNameCount " files exists with this name"
                [string]$fileurl=$item.Url
                $fileurl="$Url/$fileurl"
                $msg = "$global:Xref,$global:AssetID,`"$ListName`",`"$fileNameWithExt`",`"$fileName`",`"$documentType`",`"$fileurl`""   |  Out-File $LogFilePath -append
             
            }
           <# $itemSize = (($item.File.Length)/1024)/1024
            if($itemSize -Ge $fileSize)
            {
               $itemUrl = $item.Web.Url + "/" + $item.Url;
               Write-Host $itemUrl ", File size:: " $('{0:N2}' -f $itemSize) MB -ForegroundColor Green
            }#>
        }
     
        #[System.IO.Path]::GetFileNameWithoutExtension("Test Config.xlsx")
        <#foreach ($SubFolder in $list.RootFolder.SubFolders)
        {
            if ($SubFolder.Name -eq $FolderName)
            {
                $folderExists=$true;
                break;
            }                    
        } #>    
        <#if($folderExists -eq $false)
            {
                #Create a Folder
                $folderItem = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $FolderName)
                $folderItem.Update();            
             
                $msg = "$Url, $FolderName, folder created, $ListName"  |  Out-File $LogFilePath -append
                write-host "$Url $FolderName folder created in $ListName." -foregroundcolor green
            }
            else
            {
                $msg = "$Url, $FolderName, folder already exists, $ListName"  |  Out-File $LogFilePath -append
                write-host "$Url $FolderName folder already exists in $ListName." -foregroundcolor yellow
            }#>
    }
    $web.Dispose()

}

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
    Write-Host "Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Yellow
    LoadWSSAssembly;
    $StartTime=Get-Date
    $msg = "Xref,Loan Number,Library Name,File Name,Common Name,Document Type,Path"  |  Out-File $LogFilePath -append
   Get-SPFilesInList "<Site URL>", "<List Name>"

    $StartTime=(Get-Date) - $StartTime
    write-host "Total execution time- " $StartTime
    Write-Host "Process Completed...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Yellow

})


No comments:

Post a Comment