1

I have the below code working.

$sourceContainer = "/sitecore/content/folder/Home/Topics/To";
    $destinationContainer = "/sitecore/content/folder/Home/Topics/From";
    
    Get-ChildItem $sourceContainer | ForEach-Object {
        Move-Item -Path $_.ItemPath -Destination $destinationContainer;
    }

It carries over all the folders and files in them but will duplicate the folders vs. 1 folder with all the file that pertain to the folders. I am a PShell newbie as in first time today lol.

Given this structure:

Topics
-From
--Data
---2019
----05
-----Page-F
---2020
----04
-----Page
----06
-----Page

-To
--Data
---2019
----05
-----Page-T
---2020
----07
-----Page
----09
-----Page

Combine outcome that I want:

Topics
-To
--Data
---2019
----05
-----Page-T
-----Page-F
---2020
----04
-----Page
----06
-----Page
----07
-----Page
----09
-----Page

1 Answer 1

1

Check the following script:

$sourceContainer = "/sitecore/content/Home/from";
$destinationContainer = "/sitecore/content/Home/to";

function ProcessItem {
    param (
        [Sitecore.Data.Items.Item]$item
    )
    if((Get-Item -Path ($item.Paths.FullPath -replace $sourceContainer, $destinationContainer) -ErrorAction SilentlyContinue)) {
        Get-ChildItem $item.Paths.FullPath | ForEach-Object {
            ProcessItem -item $_
        }
    } else {
        Move-Item -Path $item.ItemPath -Destination ($item.Parent.Paths.FullPath -replace $sourceContainer, $destinationContainer)
    }
}

Get-ChildItem $sourceContainer | ForEach-Object {
    ProcessItem -item $_ -
}

What it does, it checks if the item with given name already exists under destination folder. If no, it moves it with its children, otherwise it processes all the children of the source item.

After execution is completed, items which were not necessary to be copied will be still there under the source folder.

3
  • that works! Thanks a million. I am trying to get my head around this PS syntax. I am a JS dev. Quick question and no worries if you can't - is there a way to log out what has been moved will it's running and a way to delete the left behind empty folders if empty? Like Write-Host "Item moved to: Oct 4, 2021 at 17:50
  • You can add write-host "item " $item.ItemPath " moved to " ($item.Parent.Paths.FullPath -replace $sourceContainer, $destinationContainer) line below Move-Item. And you can just delete everything in the source folder - there should be nothing left except from duplicates
    – Marek Musielak
    Oct 4, 2021 at 18:01
  • Thanks again - write out works. Oct 4, 2021 at 18:17

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.