8

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?

  • 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
  • It may be worth considering robocopy as an alternative. You can still call it from powershell if you need to. It will handle the if exists issue easily and wont require you to force-ably recursively delete your destination either, which comes with its own dangers. – Jeremy Mar 23 '18 at 16:29
  • Closely related SO question: stackoverflow.com/q/47964451/45375 – mklement0 Dec 13 '18 at 19:58
6

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
3

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

copy-item .\source\* .\destination -force -recurse
  • This only works if destination already exists. – Steven Penny Apr 16 '14 at 23:46
3

Steven Penny's answer https://superuser.com/a/742719/126444 doesn't delete original content of the target directory, just appending to it. I needed to completely replace the target folder with content of the source and created 2 functions:

function CopyToEmptyFolder($source, $target )
{
    DeleteIfExistsAndCreateEmptyFolder($target )
    Copy-Item $source\* $target -recurse -force
}
function DeleteIfExistsAndCreateEmptyFolder($dir )
{
    if ( Test-Path $dir ) {
    #http://stackoverflow.com/questions/7909167/how-to-quietly-remove-a-directory-with-content-in-powershell/9012108#9012108
           Get-ChildItem -Path  $dir -Force -Recurse | Remove-Item -force -recurse
           Remove-Item $dir -Force

    }
    New-Item -ItemType Directory -Force -Path $dir
}
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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