Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Copy-Item : Container cannot be copied onto existing leaf item.

Reply
 
Old 03-16-2009   #1 (permalink)
Steve


 
 

Copy-Item : Container cannot be copied onto existing leaf item.

I have a file rrr.config that contains the [line Backup,"c:\clean",
"C:\backup"]

I run the script :
$args = get-content -path c:\rrr.config
#Make backups
If ((select-string -path c:\rrr.config -pattern "Backup") -ne "")
{
$back = select-string -path c:\rrr.config -pattern "Backup"
foreach ($match in $back)
{
$frname = $match.ToString().Split("""")[1] + "\*"
$toname = $match.ToString().Split("""")[3]
copy-item $frname -destination $toname -Verbose -recurse -force
}
}

The folder clean contains several files and sub folders.

If the backup folder does not exisit then I get the message : "Copy-Item :
Container cannot be copied onto existing leaf item." and only the files in
the folder clean are copied not the subfolders and their content.

If the folder backup exists all the files and subfolders in clean are copied
over to the backup folder.

This happens with version 1 and 2.0 CPT 3 of powershell.

Verbose listing of output:

VERBOSE: Performing operation "Copy Directory" on Target "Item:
C:\clean\absdde Destination: C:\backup".
VERBOSE: Performing operation "Create Directory" on Target "Destination:
C:\backup".
VERBOSE: Performing operation "Copy File" on Target "Item:
C:\clean\absdde\oadist.exe Destination: C:\backup\oadist.exe".
VERBOSE: Performing operation "Copy File" on Target "Item:
C:\clean\absdde\ReadMe.txt Destination: C:\backup\ReadMe.txt".
VERBOSE: Performing operation "Copy File" on Target "Item:
C:\clean\absdde\RegClean.exe Destination: C:\backup\RegClean.exe".
Copy-Item : Container cannot be copied onto existing leaf item.
At line:14 char:17
+ copy-item <<<< $frname -destination $toname -Verbose -recurse
-force
+ CategoryInfo : InvalidArgument:
(C:\clean\CallRecording:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId :
CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Container cannot be copied onto existing leaf item.
At line:14 char:17
+ copy-item <<<< $frname -destination $toname -Verbose -recurse
-force
+ CategoryInfo : InvalidArgument: (C:\clean\sec:String)
[Copy-Item], PSArgumentException
+ FullyQualifiedErrorId :
CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand

Any ideas what is going on.



My System SpecsSystem Spec
Old 03-16-2009   #2 (permalink)
Kiron


 
 

Re: Copy-Item : Container cannot be copied onto existing leaf item.

If you qualify the path with an asterisk, Copy-Item will copy the complete dir structure with their items only if the destination already exists.
Check out this thread's first reply:
http://groups.google.com/group/micro...9b8b4424c2af22

To ensure the script executes successfully, check for the existence of the destination directory, and only append the asterisk to the source directory name if the destination directory exists.

...
foreach ($match in $back) {
$frname = $match.ToString().Split("""")[1] + '\'
$toname = $match.ToString().Split("""")[3]
if (Test-Path $frname -PathType Container) {$frname += '*'}
copy-item $frname -destination $toname -Verbose -recurse -force
}

@PSTeam,
This quirk needs to be documented, or at least explained in future Copy-Item help docs.

--
Kiron
My System SpecsSystem Spec
Old 03-16-2009   #3 (permalink)
Kiron


 
 

Re: Copy-Item : Container cannot be copied onto existing leaf item.

# mistake, change this:
if (Test-Path $frname -PathType Container) {$frname += '*'}

# to:
if (Test-Path $toname -PathType Container) {$frname += '*'}

--
Kiron
My System SpecsSystem Spec
Old 03-17-2009   #4 (permalink)
Steve


 
 

Re: Copy-Item : Container cannot be copied onto existing leaf item

Thanks for your help!

"Kiron" wrote:
Quote:

> # mistake, change this:
> if (Test-Path $frname -PathType Container) {$frname += '*'}
>
> # to:
> if (Test-Path $toname -PathType Container) {$frname += '*'}
>
> --
> Kiron
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
rename-item, move-item and special chars. PowerShell
-container in Copy-Item PowerShell
copy-item changing files attributes on network copy failures PowerShell
Copy-Item not honoring -container PowerShell
Copy-Item not honoring -container PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46