4

I need to get the count of all the media items and folders inside any media folder in media library. Then export the data to csv I am planning to write a script using SPE. Please help me on the same

For Example

  • A(Media Folder)
    • A1 (Media Item)
    • A2 (Media Item)
    • A3 (Media folder)
      • A31 (Media Item)
      • A32 (Media Item)
  • B(Media Folder)
    • B1 (Media Item)
    • B2 (Media folder)
      • B21 (Media Item)
      • B22 (Media Item)
    • B3 (Media folder)
      • B31 (Media Item)
      • B32 (Media Item)

I need to get final result as

enter image description here

Please help me on the same

1 Answer 1

6

I prepared the powershell script for you. Try it:

$mediaLibriary = Get-Item "/sitecore/media library"
$mediafoldersTemplates = "Media folder", "Node"

$root = $mediaLibriary.Children
$table = @()

foreach($rItem in $root){
    if($mediafoldersTemplates -contains $rItem.TemplateName){
        $table += ProcessChildren -item $rItem
    }
}

$table  | Format-Table -AutoSize

function ProcessChildren($item)
{
    $contOfFolders = 0
    $contOfImages = 0

    $children = $item.Children

    $result = @()
    $tempTable = @()
    foreach($chItem in $children){
        if($mediafoldersTemplates -contains $chItem.TemplateName)
        {
            $contOfFolders++
            $tempTable += ProcessChildren -item $chItem
        }else{
            $contOfImages++
        }
    }


    $objAverage = New-Object System.Object
    $objAverage | Add-Member -type NoteProperty -Name Name -Value $item.Name
    $objAverage | Add-Member -type NoteProperty -Name FolderCount -Value $contOfFolders
    $objAverage | Add-Member -type NoteProperty -Name ItemCount -Value $contOfImages

    $result += $objAverage
    $result += $tempTable

    return $result
}

You can upgrade the mediafoldersTemplates variable in the script with a template names that you consider are folder's templates.

5
  • Hi, The code you provided is working as expected. Thanks Dec 15, 2017 at 13:43
  • As a matter of preference, I like to create new objects using the syntax $average = [pscustomobject]@{"Name"=$item.Name}. SPE depends on PowerShell 3+ which is when that syntax became available. Also, I would move the function to the top of the script. Dec 15, 2017 at 14:29
  • Yes Michael, i moved the function to top of script and then only it worked. Also will take care of creating new objects using the provided syntax Dec 15, 2017 at 14:31
  • @MichaelWest Thanks guys for your comments. I am not really familiar with PowerShell. Any saggestions for improving will be appreciated. Dec 15, 2017 at 14:41
  • First, check out the #module-spe channel on the Sitecore Slack Chat. Many PowerShell users hang out there. Second, the module has a companion book doc.sitecorepowershell.com. Third, the book Professional Sitecore 8 Development has a chapter (10) I wrote which covers using the SPE module. Dec 15, 2017 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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