Organize media files using Sitecore PowerShell



Sometimes you work on project that contains big modules like "News & Media" and it contains many Media Files,
 
organizing Items easy using Buckets, but what about the Media files?!


In this Blog Post I will provide you with a script that help you to generate media folders from 0-9 & a-z
i.e.














Dependencies:


The Script:

The below script will help you generate Alpha Numeric Folders and move the files under those folders


$alphaNumeric=@() 65..90|foreach-object{$alphaNumeric+=[char]$_} # Add the characters A-Z to the Array 48..57|foreach-object{$alphaNumeric+=[char]$_} # Add the characters 0-9 to the Array # The folder in which you want to organize the media $root = "master:/sitecore/media library/{your path}" # Generate folders based on the alpha numeric array foreach ($item in $alphaNumeric) { # Skip if the folder already exists if (@(Get-Item -Path "master:" -Query $root"/"$item).count -gt 0) { Continue # Create new item } else { New-Item -Path $root"/"$item -ItemType "/sitecore/templates/System/Media/Media folder" } } # Get all items under the root folder $mediaItems= Get-ChildItem -Path $root # move images to the correct folder based on first character of the media Item name foreach($item in $mediaItems){ # Check if the item is not media folder if($item.TemplateId -ne "{FE5DD826-48C6-436D-B87A-7C4210C7413B}"){ $fChar= $item.Name[0] $newItemPath=$root+"/"+$fChar+"/"+$item.Name # Check if the Item already exists in the path if (Test-Path -Path $newItemPath){ Continue } Move-Item -Path $item.FullPath -Destination $root"/"$fChar } }

Comments

Popular posts from this blog

Sitecore Multi list Code Data Source

C# Numeric types Overflow