3

I am trying to move images from one folder to another. The images folder contains like 1000 images. i want to move the first 100 images to one folder the other 100 images to another folder and the other 100 images to another folder. i found a script that moves all the images. but i need to move by 100. even if i get the code for first 100 i can change the path in the code(target Path). Thanks

$sourcePath = "/sitecore/media library/Default Website" 
$targetPath = "/sitecore/media library/dev" 
$optionalTemplateNameToMatch = "" 
# optional template get children line 
# Get-ChildItem $sourcePath -Recurse | Where-Object { $_.TemplateName -match $optionalTemplateNameToMatch } | ForEach-Object {    
 
Get-ChildItem $sourcePath -Recurse | ForEach-Object {  
    $name = $_.Name 
    if(![string]::IsNullOrEmpty($name)) 
    { 
        Move-Item -Path $_.ItemPath -Destination $targetPath; 
        Write-Host "Item moved to: "$_.ItemPath; 
    } 
    else 
    { 
        Write-Host "Couldn’t move Item because empty name: " $_.Id; 
    } 
}
1
  • Have you considered using item buckets? Commented Nov 8, 2022 at 0:24

1 Answer 1

2

If you are happy to update the target path each time, then all you need to do is add a counter into your script, then check its less than 100 each time through the for loop.

$sourcePath = "/sitecore/media library/Default Website" 
$targetPath = "/sitecore/media library/dev" 
$counter = 0 
 
Get-ChildItem $sourcePath -Recurse | ForEach-Object {  

    $name = $_.Name 
    if((![string]::IsNullOrEmpty($name)) -and ($counter -lt 100) -and ($_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder))) 
    { 
        Move-Item -Path $_.ItemPath -Destination $targetPath; 
        Write-Host "Item moved to: "$_.ItemPath; 
        $counter+=1
    } 
    else 
    { 
        Write-Host "Couldn’t move Item because empty name: " $_.Id; 
    } 
}

Note I also added in a check to confirm the templateId for the item is not a folder.

When testing out powershell scripts in sitecore, it is useful to first do a test run to see what would be effected without moving the items themselves.

The script below, splits the actions into two methods Find-MediaItems and Move-MediaItems. You can then call Find-MediaItems first and output the results to table.

If you are happy with what is going to be moved, you then need to uncomment the line #MoveItem($item), and comment out the line above.

function Find-MediaItems {
    $items = Get-ChildItem -Path $sourcePath.Paths.FullPath | 
    Where-Object { ($_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder) }
        
    foreach($item in $items) {
        $item;
        #MoveItem($item)
    }
}

function MoveItem($item)
{
    $name=$item.Name
    if((![string]::IsNullOrEmpty($name)) -and ($counter -lt 100))) 
    { 
        Move-Item -Path $_.ItemPath -Destination $targetPath; 
        Write-Host "Item moved to: "$_.ItemPath; 
        $counter+=1
    } 
    else 
    { 
        Write-Host "Couldn’t move Item because empty name: " $_.Id; 
    } 
}

$props = @{
    InfoTitle = "Media items to move"
    InfoDescription = "Lists all media items that the script will eventually move"
    PageSize = 25
}

Find-MediaItems |
    Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
        @{Label="Updated"; Expression={$_.__Updated} },
        @{Label="Updated by"; Expression={$_."__Updated by"} },
        @{Label="Created"; Expression={$_.__Created} },
        @{Label="Created by"; Expression={$_."__Created by"} },
        @{Label="Path"; Expression={$_.ItemPath} }
        
Close-Window

The above is something I found myself when in similar situation to you, then have edited to suit over time (cant recall original source).

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.