Thread: Copy-Item
View Single Post
Old 08-09-2007   #2 (permalink)
Oisin Grehan
Guest


 
 

Re: Copy-Item

On Aug 8, 8:12 pm, "VRSki" <VR...@newsgroup.nospam> wrote:
> Please help!
>
> I am trying to use Copy-Item to recursively copy a directory. Here is the
> example:
>
> Initial Directory Structure
>
> 1.
> c:\
> |--Test [folder]
> | |
> |--src [folder]
> |
> |-- A [folder]
> | |
> | |-- 1.txt [file]
> |
> |-- 2.txt [file]
>
> Here is what I need to happen
>
> 2.
> c:\
> |--Test [folder]
> | |
> |--dest [folder]
> | |
> | |-- A [folder]
> | | |
> | | |-- 1.txt [file]
> | |
> | |-- 2.txt [file]
> |
> |--src [folder]
> |
> |-- A [folder]
> | |
> | |-- 1.txt [file]
> |
> |-- 2.txt [file]
>
> So, here is the command I am using:
>
> copy-item c:\test\src\ c:\test\dest\ -recurse -force
>
> Great! This works! But now, if I try to execute exactly the same command
> (expecting no changes to the directory structure) I get an addtional
> directory \src under \dest
>
> 3.
> c:\
> |--Test [folder]
> | |
> |--dest [folder]
> | |
> | |-- A [folder]
> | | |
> | | |-- 1.txt [file]
> | |
> | |-- 2.txt [file]
> |
> |--src [folder]
> |
> |-- A [folder]
> | |
> | |-- 1.txt [file]
> |
> |-- 2.txt [file]
> |
> |--src [folder]
> |
> |-- A [folder]
> | |
> | |-- 1.txt [file]
> |
> |-- 2.txt [file]
>
> Fine. I can sort of understand the difference in copying a folder when the
> destination exists vs. when it's not there...
>
> So, I delete \src from under \dest and try this:
> copy-item c:\test\src\* c:\test\dest\ -recurse -force
>
> It works! I get what I expected -- sample 2. So, just to verify it, I delete
> folder \a from under \dest and try the same command again.
> copy-item c:\test\src\* c:\test\dest\ -recurse -force
>
> Works again! So just to test the whole thing from scratch I delete the
> entire folder \dest and run exactly the same command:
> copy-item c:\test\src\* c:\test\dest\ -recurse -force
>
> Oh no!! Now the folder \A (I am expecting to be under \dest) is missing, but
> 2.txt is there: all the directories were flattened.
>
> 4.
> c:\
> |--Test [folder]
> | |
> |--dest [folder]
> | |
> | |-- 1.txt [file]
> | |
> | |-- 2.txt [file]
> |
> |--src [folder]
> |
> |-- A [folder]
> | |
> | |-- 1.txt [file]
> |
> |-- 2.txt [file]
>
> I run exectly the same command and now I get my folder \A under \dest.
>
> So, what am I doing wrong? Is Copy-Item even meant to copy folders? All I
> want is the consistent behavior (with preferably the same command syntax):
> dest is created if it doesn't exist, and if it does exist, all \src files
> and directories should be under \dest.
>
> Please help!
>
> Thanks,
> VR


Hi VR,

You say:

>> Works again! So just to test the whole thing from scratch I delete the
>> entire folder \dest and run exactly the same command:
>> copy-item c:\test\src\* c:\test\dest\ -recurse -force


However, this is NOT the same as your initial command:

>> So, here is the command I am using:
>> copy-item c:\test\src\ c:\test\dest\ -recurse -force


The difference is the asterisk.

In the former case, you are copying everything within src\ e.g. the
source is a _collection_, and the first item retrieved with the
wildcard is then used to create "dest" because it doesn't exist yet.
The remaining items in the collection are then placed in dest (which
create from "A"). Yes, it's counterintuitive; some might say
unexpected or even useless behaviour.

In the latter case (your original command), the source resolves to a
single item, the directory "src." This is then mapped to the as yet
nonexistant "dest" and you should get the expected behaviour.

This behaviour IMO, is an artifiact of the provider and object
pipeline model that powershell uses. You have to get used to the idea
that the navigational context is a bit weird compared to other shells;
it's explicit as opposed to implicit, since the filesystem is one of
many contexts that can be used. This brings with it some weird
behaviours since the targets of any given path are handled with a
combination of powershell's generic grammar and the command itself, as
opposed to being entirely handled by the command in shells like
command.com/cmd.exe.

Hope this helps,

- Oisin

My System SpecsSystem Spec