Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy folder using Copy-Item - different behaviour

Tags:

powershell

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

My source folder c:\base contains some files:

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

I use the following command:

Copy-Item c:\base c:\target -recurse

if the target folder (c:\target) 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?
like image 816
Ivan Avatar asked May 08 '11 08:05

Ivan


People also ask

What are the different ways to copy files and folders?

Select the file you want to copy by clicking on it once. Right-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V .


1 Answers

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.

like image 193
manojlds Avatar answered Oct 15 '22 13:10

manojlds