I'm supposed to move data from an old server to a new one while changing the structure.
Example:
In the old structure, the orders of a company are in a single folder. In the new structure, all orders are to be migrated directly under the orders folder. The name consists of the order number + separator + company name + name.
current task is the folders that were successfully copied from "C:\test\old\Data\Werkstatt\06. Aufträge\%company%\" in the folder "C:\test\old\Data\Werkstatt\06. Aufträge\%company%\Z_Transfered" to move.
I tried:
$sourcepath = "C:\test\old\Data\Werkstatt\06. Aufträge"
$basepath = "C:\test\new\Data\06. Aufträge"
$Orders = Get-ChildItem -Path $sourcepath -Directory -Recurse -Depth 1
ForEach ($Order in $Orders ) {
if ($Order.name.StartsWith("20")){
$NewFolderName = "{0}\{1}" -f ($BasePath),($order.Name.insert(6,"_" + $order.parent.Name))
$NewFolder = New-Item -Path $NewFolderName -ItemType "directory"
Copy-Item -Path "$($Order.FullName)\*" -Destination $NewFolder -Recurse -Force
#under construction
$MoveFolderName = Join-Path -path $sourcepath -ChildPath $order.parent | Join-Path -ChildPath "Z_Transfered"
if (-not (Test-Path $MoveFolderName))
{
New-Item -Path $MoveFolderName -ItemType "directory"
}
$MoveFolder = $MoveFolderName +"\" + $order.name
Move-Item -Path "$($Order.FullName)\*" -Destination $MoveFolder
$error > $sourcepath"\error.log"
}
}
I adapted the code I got yesterday. Currently I have one more error in the code.
Copy content to the right place works now. Currently he creates the folder Z_Transfered below the customer folder. The problem seems to be with Move-Item. Currently, the folder is not moved to Z_Transfered, only the files under the customer folder are moved and, unfortunately, also renamed. Can someone help me here please?
You can get the parentfolder of a folder by putting .parent
to the pathobject > $path.parent
. And you can even get the grandparent that way > $path.parent.parent
.
So all that is left is a little string manipulation to get the new folder name:
$sourcepath = "d:\test\Data\Orders" # Adjust this
$BasePath = "d:\test\Data\Orders" # Adjust this
$Orders = Get-ChildItem -Path $sourcepath -Directory -Recurse -Depth 2
ForEach ($Order in $Orders ) {
if ( $Order.parent.parent.Name -eq "Orders" ){
$NewFolderName = "{0}\{1}" -f ($BasePath),($order.Name.replace("_","_$($Order.parent)_"))
$NewFolder = New-Item -Path $NewFolderName -ItemType "directory"
Move-Item -Path "$($Order.FullName)\*" -Destination $NewFolder -WhatIf
}
}
In Powershell 5 Get-ChildItem
got the -Depth
parameter so you can limit it to search only two levels below your $sourcepath
. The replace will look for _
in the foldername so make sure thery all contain only one.