0

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) - 8MB

A1 (Media Item) - 2MB

A2 (Media Item) - 2 MB

A3 (Media folder) - 4MB

A31 (Media Item) - 2 MB

A32 (Media Item) - 2MB

B(Media Folder) - 3MB

B1 (Media Item) - 1MB

B2 (Media folder) - 2MB

B21 (Media Item) - 1MB

B22 (Media Item) - 1MB

I am using below script found in one of the answer but I want the size of the subfolder also as shown above:

$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
}

1 Answer 1

2
function ProcessChildren($item)
{      
  $foldersize = 0
  $allItems = Get-ChildItem -Path $item.Paths.FullPath -Recurse
  $allItems | ForEach-Object {        
      if(-Not($mediafoldersTemplates -contains $_.TemplateName)){
        $foldersize += $_.Size
      }
  }
  $folderItemSize = $foldersize/1024/1024;
  $folderRoundedValue = "{0:n3}" -f $folderItemSize
  Write-Host "Media Folder Name = $($item.Name) - Size  $folderRoundedValue MB"
  $childItems = Get-ChildItem -Path $item.Paths.FullPath
  foreach($chmediaItem in $childItems)
  {
      if(-Not($mediafoldersTemplates -contains $chmediaItem.TemplateName))
      {
          $chMediasize = $chmediaItem.Size/1024/1024
          $mediaItemSizeRoundedValue = "{0:n3}" -f $chMediasize
          Write-Host "Media Item Name = $($chmediaItem.Name) - Size  $mediaItemSizeRoundedValue MB"
      }
  } 
}
$mediaLibriary = Get-Item "/sitecore/media library/Project/Hackathon"
$mediafoldersTemplates = "Media folder", "Node"
$root = Get-ChildItem -Path $mediaLibriary.Paths.FullPath -Recurse
$root | ForEach-Object {    
  if($mediafoldersTemplates -contains $_.TemplateName)
  {
      ProcessChildren -item $_
  }
}

Try this answer once and let me know if it work. You can extend or reuse this code to get your result.

3
  • I tried but it is asking for Process value and after that it is throwing error: ProcessChildren : The term 'ProcessChildren' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:9 char:19 + $table += ProcessChildren -item $rItem + ~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (ProcessChildren:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    – Akshay
    Commented Nov 23, 2020 at 7:06
  • 1
    I updated the example because I saw the issue too. Turns out there was a newline that needed to be removed. Commented Nov 23, 2020 at 14:25
  • Its working fine. thank you so much for your help :)
    – Akshay
    Commented Nov 24, 2020 at 4:24

Your Answer

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

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