3

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.

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The /mir option to only copy new or different files, and delete from destination where removed from source is a very efficient way to update the destination.
@Richard so would the full thing be robocopy D:\Test1 D:\Test2 /s /mir /it ??
@АртёмЦарионов Tweaked files are files that have the same size and timestamp, but different attributes (e.g. source file was changed to hidden). See this answer on SU for an explanation of robocopy's terminology.
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.