|
Copy-Item 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 |