0
votes

I'm supposed to move data from an old server to a new one while changing the structure.

Example:

  • old: "C:\test\old\Data\Werkstatt\06. Aufträge\companyA\200031_Signs"
  • new: "C:\test\new\Data\06. Aufträge\companyA\200031_companyA_Signs"

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?

2
are there other folders inside the order folder (inside 20031_Signs)?T-Me
in these and most other cases yesSven
is Powershell 5 available?T-Me
yes is availableSven
The sample code you posted would sort-of re-create the childfolders from $sourcepath (which won't work correctly since object properties are not expanded inside strings). It doesn't even attempt to produce the desired new names. Please try a little harder.Ansgar Wiechers

2 Answers

0
votes

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.

0
votes

it works :)

many thanks for your help. The script works now as I need it.

$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
    $MoveFolderName = Join-Path -path $sourcepath -ChildPath $order.parent | Join-Path -ChildPath "Z_Transfered"
     if (-not (Test-Path $MoveFolderName))
       {
        $MoveFolder = New-Item -Path  $MoveFolderName -ItemType "directory"
       }
    Move-Item -path "$($Order.FullName)\" -Destination $MoveFolderName -force
    $error > $sourcepath"\error.log"
 }
}