Alex Gordon
Alex Gordon

Reputation: 60691

How to copy entire directory structure?

I am copying 10,000 files from one directory to another directory.

Both directory structures have the same files; however, the sizes might be different.

How do I force overwriting on files that have different sizes and DO NOT copy files that are the same size?

So far I have this:

$source = 'D:\Test1'
$destination = 'D:\test2'
$exclude = @('*.db','test2.log')
$sourcelist = Get-ChildItem $source -exclude $exclude
foreach ($file in $sourcelist){
  $result = test-path -path "$destination\*" -include $file
  if ($result -like "False"){
    #uncomment the next line to see the value being passed to the $file parameter
    #Write-Host $file
    Copy-Item "$file" -Destination "$destination"
  }
}

I think that will copy files that do not exist on the destination. However, I also want to copy files that DO exist but where their size differs.

Upvotes: 3

Views: 5500

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

Use robocopy instead of trying to re-invent it in PowerShell.

robocopy D:\Test1 D:\Test2 /s

If you also want to include files where only attributes differ ("tweaked files"), add /it as well.

Upvotes: 10

billy
billy

Reputation: 21

I think check $sourcelist files LastWrite Time Stamps and then compare it to the destination files time stamps.

Upvotes: -1

Related Questions