![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Copy-Item : Container cannot be copied onto existing leaf item. | PowerShell | |||
| Can the copy-item cmdlet be used to copy public folders to C: ? | PowerShell | |||
| copy-item changing files attributes on network copy failures | PowerShell | |||
| Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ?? | PowerShell | |||
| Copy-Item or Copy-ItemProperty and Remote Registry. | PowerShell | |||