比较目录和子目录并基于MD5哈希替换文件 [英] Compare directories and sub directories and replace files based on MD5 hash
问题描述
具有一个基于MD5哈希进行比较的比较脚本.注意到它在做奇怪的事情.
Have a comparison script that compares based on MD5 hash. Noticed that it's doing odd things.
$Source= "D:\Folder1"
$Destination = "D:\folder2"
get-childitem $Source -Recurse | foreach {
#Calculate hash using Algorithm MD5 reference http://www.tomsitpro.com/articles/powershell-file-hash-check,2-880.html
Write-Host "Copying $($_.fullname) to $Destination" -ForegroundColor Yellow
$OriginalHash = Get-FileHash -Path $_.FullName -Algorithm MD5
#Now copy what's different
$replacedfile = $_ | Copy-Item -Destination $Destination -Force -PassThru
#Check the hash sum of the file copied
$copyHash = Get-FileHash -Path $replacedfile.FullName -Algorithm MD5
#compare them up
if ($OriginalHash.hash -ne $copyHash.hash) {
Write-Warning "$($_.Fullname) and $($replacedfile.fullname) Files don't match!"
}
else {
Write-Host "$($_.Fullname) and $($replacedfile.fullname) Files Match" -ForegroundColor Green
}
} #Win!
该脚本可以正常工作,直到在子文件夹中找到差异为止.然后由于某种原因我似乎找不到,它将不同的项目复制到顶层和子层中……..我真的很笨,我看不到问题,需要第二对的眼睛.
The script works fine, until it finds a difference in a sub folder. It then for some reason i can't seem to find, it copies the different item to BOTH the top level and the sub level.....I am being really dumb and I can't see the issue, need a second pair of eyes.
示例
File Test.Txt is in Source\SubFolder
脚本运行,并将Test.Txt放入Destination \ Subfolder AND Destination .....
Script runs and puts Test.Txt in Destination\Subfolder AND Destination.....
有什么想法吗?
推荐答案
行$replacedfile = $_ |
是错误,这里$ _仅包含名称而不包含子目录,并复制到$ Destination.
The line $replacedfile = $_ |
is a fault, here $_ contains only the name without the subdir, and is copied to $Destination.
替换行:
$replacedfile = $_ | Copy-Item -Destination $Destination -Force -PassThru
具有:
$replacedfile = $_.Fullname -replace [RegEx]::escape($Source),$Destination
这将替换不同的基础部分,而使当前源子目录保持完整.
[RegEx] :: escape($ Source)是必需的,因为源字符串包含
反斜杠,replace函数将其解释为转义字符.
This replaces the differing base part leaving the curent source sub dir intact.
The [RegEx]::escape($Source) is necessary because the source string contains
backslashes which would be interpreted as an escape char by the replace function.
EDIT (完整脚本)以避免歧义:
EDIT Complete script to avoid ambiguities:
$Source= "D:\Folder1"
$Destination = "D:\folder2"
get-childitem $Source -Recurse | foreach {
#Calculate hash using Algorithm MD5 reference http://www.tomsitpro.com/articles/powershell-file-hash-check,2-880.html
$SrcFile = $_.FullName
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5
$DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination
Write-Host "Copying $SrcFile to $DestFile" -ForegroundColor Yellow
if (Test-Path $DestFile) {
#Check the hash sum of the file copied
$DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
#compare them up
if ($SrcHash.hash -ne $DestHash.hash) {
Write-Warning "$SrcFile and $DestFile Files don't match!"
Copy-Item $SrcFile -Destination $DestFile -Force
} else {
Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor Green
}
} else {
Copy-Item $SrcFile -Destination $DestFile -Force
}
}
这篇关于比较目录和子目录并基于MD5哈希替换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!