Reputation: 53
ht = @{}
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\BCC"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatiON" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\BBB"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatiON" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\BAA"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatiON" }
$ht.Add($o.from, $o)
#
foreach($server in $ht.keys ) {
copy-item $ht.Item($server).from -destination $ht.Item($server).to -Recurse ;
}
sleep 5
$ht = $null
sleep 5
$ht = @{}
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BCC"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BCC\Processed" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BBB"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BBB\Processed" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BAA"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BAA\Processed" }
$ht.Add($o.from, $o)
# the loop
foreach($server in $ht.keys ) {
gci | Move-Item $ht.Item($server).from -Destination $ht.Item($server).to -Force ;
}
The last loop isn't working, I keep getting an error with access denied , after everything runs I have the last part to actually move from one place to another. Move-Item : Access to the path 'C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BBB' is denied. At line:47 char:13 + Move-Item $ht.Item($server).from -Destination $ht.Item($s ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Users\nicola...\destination\BCC:DirectoryInfo) [Move-Item], IOException + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
I really can't wrap my head around this one, why do I keep getting the error. Also I have tried with the -Force option but from that ht of 3 items the first one doesn't go through .
This is the entire code.
Upvotes: 0
Views: 468
Reputation: 24071
This happens, as the latter loop is trying to move the source directory itself. By adding the -WhatIf
switch the behavior becomes clear. Like so,
foreach($server in $ht.keys ) {
Move-Item -whatif $ht.Item($server).from -Destination $ht.Item($server).to -Force ;
}
# Line breaks added for readability
What if: Performing the operation "Move Directory" on target
"Item: C:\temp\destinatie\BCC
Destination: C:\temp\destinatie\BCC\Processed\BCC".
Consider fixing the gci
part, like so,
foreach($server in $ht.keys ) {
gci $ht.Item($server).from | % { Move-Item -whatif $_.FullName -Destination $ht.Item($server).to }
}
What if: Performing the operation "Move Directory" on target "Item: C:\temp\destinatie\BCC\Processed Destination: C:\temp\destinatie\BCC\Processed\Processed".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\1.txt Destination: C:\temp\destinatie\BCC\Processed\1.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\2.txt Destination: C:\temp\destinatie\BCC\Processed\2.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\3.txt Destination: C:\temp\destinatie\BCC\Processed\3.txt".
Much better. But wait, it's still trying to move the Processed
and that we can't have. Let's exclude that one and try again.
foreach($server in $ht.keys ) {
gci $ht.Item($server).from -exclude "processed" | % { Move-Item -whatif $_.FullName -Destination $ht.Item($server).to }
}
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\1.txt Destination: C:\temp\destinatie\BCC\Processed\1.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\2.txt Destination: C:\temp\destinatie\BCC\Processed\2.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\3.txt Destination: C:\temp\destinatie\BCC\Processed\3.txt".
Upvotes: 2