Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - Copy folder using Copy-Item - different behaviour

I want copy whole folder to another folder using Copy-Item.

My source folder c:ase contains some files:

├───base    
│       file1.txt
│       file2.txt

I use the following command:

Copy-Item c:ase c:arget -recurse

if the target folder (c:arget) exists, the command copies source folder exactly as I want:

├───target
│   └───base    
│           file1.txt
│           file2.txt

If the target folder doesn't exist, the command creates target folder (exactly as I want), but now it copies only the content of the source folder (without the base folder):

├───target    
│       file1.txt
│       file2.txt
  • Why does it happen?
  • How can I use this copy command (additional keys?) without create the target folder before?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a known bug ( feature some would say):

http://groups.google.com/group/microsoft.public.windows.powershell/msg/3327f2d1544e7fb6?as_umsgid=6CAEDDC7-0F1A-4B21-8FB0-E6102A16EB51@microsoft.com

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...