Windows Vista Forums
Vista Forums Home Join Vista Forums Donate 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 Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Copy-Item

Reply
 
LinkBack Thread Tools Display Modes
Old 08-08-2007   #1 (permalink)
VRSki
Guest


 
 

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



My System SpecsSystem Spec
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
Old 08-10-2007   #3 (permalink)
VRSki
Guest


 
 

Re: Copy-Item

Oisin,

Thank you for your post.

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:
>


I understand and agree, it's not the original command. I was merely pointing
out the fact that at that given step I have achieved the desired result.

Now, the question still stands. What would be the syntax of the command to
copy the \src directory into \dest so that the outcome would be the same
regardless of whether \dest exists or not.

I guess I can accept the fact that the syntax may differ between the two
cases, but can somebody tell me what would that syntax be?

Thanks,
Vic




"Oisin Grehan" <oising@gmail.com> wrote in message
news:1186672886.165245.259790@w3g2000hsg.googlegroups.com...
> 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
Old 08-11-2007   #4 (permalink)
Kiron
Guest


 
 

Re: Copy-Item

To work around this bug?/feature? in Copy-Item you can check the existence
of the destination then execute accordingly:

if (test-path c:\test\dest\)
{
copy-item c:\test\src\* c:\test\dest\ -recurse -force
}
else
{
copy-item c:\test\src\ c:\test\dest\ -recurse -force
}

But if you want to save some typing and time, this function will get the
results you expect.
First it deletes the ending asterisk of the source path because if the
destination does not exist Copy-Item ignores the source's child
subdirectory. (If there is no asterisk at the end of the source path it is
unchanged)
Then it checks for the existence of the destination directory, if it exists
the switch statement adds the asterisk to the source path.
Finally Copy-Item gets the right variation of the source path and copies the
child items.

function xCopy
(
[string]$src = $(throw "Specify the source directory"),
[string]$dest = $(throw "Specify the destination diirectory")
)
{
$src = $src -replace '\*$'
if (test-path $dest)
{
switch -regex ($src)
{
'\\$' {$src = "$src*"; break}
'\w$' {$src = "$src\*"; break}
default {break}
}
}
copy-item $src $dest -recurse -force
}

You can call the function like:
xCopy c:\test\src c:\test\dest\
xCopy c:\test\src\ c:\test\dest\
xCopy c:\test\src\* c:\test\dest\

....or minimally naming the parameters (position does not matter)
xCopy -d c:\test\dest\ -s c:\test\src
xCopy -d c:\test\dest\ -s c:\test\src\
xCopy -d c:\test\dest\ -s c:\test\src\*

--
Kiron

My System SpecsSystem Spec
Old 08-13-2007   #5 (permalink)
VRSki
Guest


 
 

Re: Copy-Item

Thanks Kiron.

"Kiron" <Kiron@HighPlainsDrifter.com> wrote in message
news:6CAEDDC7-0F1A-4B21-8FB0-E6102A16EB51@microsoft.com...
> To work around this bug?/feature? in Copy-Item you can check the existence
> of the destination then execute accordingly:
>
> if (test-path c:\test\dest\)
> {
> copy-item c:\test\src\* c:\test\dest\ -recurse -force
> }
> else
> {
> copy-item c:\test\src\ c:\test\dest\ -recurse -force
> }
>
> But if you want to save some typing and time, this function will get the
> results you expect.
> First it deletes the ending asterisk of the source path because if the
> destination does not exist Copy-Item ignores the source's child
> subdirectory. (If there is no asterisk at the end of the source path it is
> unchanged)
> Then it checks for the existence of the destination directory, if it
> exists
> the switch statement adds the asterisk to the source path.
> Finally Copy-Item gets the right variation of the source path and copies
> the
> child items.
>
> function xCopy
> (
> [string]$src = $(throw "Specify the source directory"),
> [string]$dest = $(throw "Specify the destination diirectory")
> )
> {
> $src = $src -replace '\*$'
> if (test-path $dest)
> {
> switch -regex ($src)
> {
> '\\$' {$src = "$src*"; break}
> '\w$' {$src = "$src\*"; break}
> default {break}
> }
> }
> copy-item $src $dest -recurse -force
> }
>
> You can call the function like:
> xCopy c:\test\src c:\test\dest\
> xCopy c:\test\src\ c:\test\dest\
> xCopy c:\test\src\* c:\test\dest\
>
> ...or minimally naming the parameters (position does not matter)
> xCopy -d c:\test\dest\ -s c:\test\src
> xCopy -d c:\test\dest\ -s c:\test\src\
> xCopy -d c:\test\dest\ -s c:\test\src\*
>
> --
> Kiron
>



My System SpecsSystem Spec
Reply

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can the copy-item cmdlet be used to copy public folders to C: ? JoshGfromPortland PowerShell 8 06-09-2008 09:03 PM
copy-item changing files attributes on network copy failures jas PowerShell 4 01-31-2008 12:39 PM
Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ?? Marc Scheuner PowerShell 15 10-26-2007 02:41 AM
Copy-Item JW PowerShell 4 05-03-2007 04:19 PM
Copy-Item or Copy-ItemProperty and Remote Registry. Brandon Shell PowerShell 1 01-09-2007 10:51 AM


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 47 48 49 50 51 52 53