两个目录的比较

Comparison of two directories

我有一个脚本可以比较两个非常相似的目录,看看哪个目录有更新的、更新的文件。这两个路径具有相同的文件,但路径名称是略有不同的位置。这两个文件夹的文件数量差不多,可能比另一个少两三个。

$path1 = "E:\docs\training\files"
$path2 = "D:\docs\training - Copy\files"
$outdatedFiles = @()

foreach($file in $Folder1)
{
    foreach($file2 in $Folder2)
    {
        if($file2.BaseName -match $file.BaseName)
        {
            if($file.LastWriteTime -gt $file2.LastWriteTime)
            {
                $Result = "" | Select OutDatedFile,LastWriteTime
                $Result.OutDatedFile = $file2.FullName
                $Result.LastWriteTime = $file2.LastWriteTime
                $outdatedFiles += $Result
            }
        }
    }
}

$outdatedFiles 数组中,我得到的文件并不比其他目录中的对应文件新。我认为这可能是由于我在 if 语句中进行了比较,我尝试了 -match-contains-ccontains 以查看其中任何一个是否能满足我的需求.都没有用。可能是 foreach 不起作用,因为每个文件夹中的文件数量略有不同。有什么建议吗?

编辑

我尝试构建一个散列,但没有找到所有更新的文件:

$outdatedFiles = @()
foreach($file in $Folder1)
{
    foreach($file2 in $Folder2)
    {
        if($file2.Name -like $file.Name)
        {
            #compare hash here
            $Hash2 = Get-FileHash $file2.FullName -Algorithm SHA256
            $Hash1 = Get-FileHash $file.FullName -Algorithm SHA256
            if($Hash2.Hash -ne $Hash1.Hash)
            {
                $Result = "" | Select OutDatedFile,LastWriteTime
                $Result.OutDatedFile = $file2.FullName
                $Result.LastWriteTime = $file2.LastWriteTime
                $outdatedFiles += $Result
            }
        }
    }
}

编辑

这是我的解决方案

$Differences1 = @()
foreach($file in $Folder1)
{
    foreach($file2 in $Folder2)
    {
        <#Trim path then compare#>
        #File1
        $file1part1 = ($file.FullName).Split("\")[-2]
        $file1part2 = ($file.FullName).Split("\")[-1]
        $newPath1 = $file1part1 + "\" + $file1part2
        #File2
        $file2part1 = ($file2.FullName).Split("\")[-2]
        $file2part2 = ($file2.FullName).Split("\")[-1]
        $newPath2 = $file2part1 + "\" + $file2part2
        if($newPath1 -like $newPath2)
        {
            $Differences1 += Compare-Object (gci $file2.FullName) -DifferenceObject (gci $file.FullName) -Property LastWriteTime -PassThru | Select Name,FullName,LastWriteTime | Sort-Object -Property Name
        }
    }
}
if($Differences1 -ne $null)
{
    $Differences1 | Out-File $textFile -Append
}
else
{
    "No folders have different modified dates" | Out-File $textFile -Append
}

最后,解决方案比我想要的更复杂,或者我只是那样做的。 问题是我有多个具有相同名称和相似路径的文件,因为在子文件夹中的名称相同。我必须 trim 路径才能获得更好的比较:

$Differences1 = @()
foreach($file in $Folder1)
{
    foreach($file2 in $Folder2)
    {
        <#Trim path then compare#>
        #File1
        $file1part1 = ($file.FullName).Split("\")[-2]
        $file1part2 = ($file.FullName).Split("\")[-1]
        $newPath1 = $file1part1 + "\" + $file1part2
        #File2
        $file2part1 = ($file2.FullName).Split("\")[-2]
        $file2part2 = ($file2.FullName).Split("\")[-1]
        $newPath2 = $file2part1 + "\" + $file2part2
        if($newPath1 -like $newPath2)
        {
            $Differences1 += Compare-Object (gci $file2.FullName) -DifferenceObject (gci $file.FullName) -Property LastWriteTime -PassThru | Select Name,FullName,LastWriteTime | Sort-Object -Property Name
        }
    }
}
if($Differences1 -ne $null)
{
    $Differences1 | Out-File $textFile -Append
}
else
{
    "No folders have different modified dates" | Out-File $textFile -Append
}