简体   繁体   中英

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.

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.

我想检查$ sourcelist文件LastWrite时间戳,然后将它与目标文件时间戳进行比较。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM