Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a directory Source with some files in it, which I would like to copy to a folder Destination. Destination may exist, and it may have files in it already. Any files with the same name as those in Source should be overwritten.

If I run this in Powershell:

Copy-Item Source Destination -Force -Recurse
Copy-Item Source Destination -Force -Recurse
Copy-Item Source Destination -Force -Recurse

Then the first line creates the folder .\Destination and copies .\Source into it, which is what I'd like to repeat for the next time. However, the second line copies .\Source into the new .\Destination folder (creating .\Destination\Source) instead, then the third line overwrites .\Destination\Source again.

How can I make it act like in the first case all the time? That is, overwrite .\Destination instead of copying into it?

share|improve this question
    
Can you clarify? you want to copy the contents of "source" in to "destination"? and not copy the folder "source" into "destination"? –  BroScience Jan 31 '13 at 15:25
    
Yes, that's correct. The "destination" folder may not exist, so I want it to be created if it doesn't exit. –  Douglas Jan 31 '13 at 17:22

3 Answers 3

up vote 1 down vote accepted

So the problem is that

cp -r -fo foo bar

only works if bar does not exist and

cp -r -fo foo/* bar

only works if bar exists. So to work around, you need to make sure bar exists before doing anything

md -f bar
cp -r -fo foo/* bar
share|improve this answer

If you only want to copy contents of the "source" folder use

copy-item .\source\* .\destination -force -recurse
share|improve this answer
    
This only works if destination already exists. –  Steven Penny Apr 16 at 23:46

Let's say that you Have the following structure of Directories

  • root

    • folder_a
      • a.txt
      • b.txt
      • c.txt
    • folder_b
      • a.txt
      • b.txt

    In The root folder you can achieve the results you want by the following sequence of commands:

    $files = gci ./folder_b -name
    cp ./folder_a/*.txt -Exclude $files ./folder_b

Only c.txt will be copied

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.