如何比较powershell中3个目录的内容

时间:2016-07-12 11:22:31

标签: powershell directory comparison powershell-v2.0 powershell-v3.0

在研究了一段时间之后,我发现很多人都试图做同样的事情,但我还没有找到一个完整的答案,所以我希望有人可以帮助我们!

我想要做的是给出2个目录,A和B,并将A内的所有内容与B内的所有内容进行比较,如果B中有任何不在A中的文件或文件夹,则生成详细说明路径的输出文件到了B中而不是A的项目。从这里,Id喜欢使用项目列表,并说明这些路径中的任何项目是否包含与2个目录中的文件不同的内容(名称将相同,因此文档\ folder \ B.txt包含与desktop \ folder \ B.txt不同的内容)生成另一个列表,显示不同的内容或显示不同项目的文件路径,如果我无法显示不同的文本。

我已经考虑过使用.hash这样做,但我不确定这是不是最好的方法呢?类似的东西:

$SourceDocs = Get-ChildItem –Path C:\Documents1 | foreach  {Get-FileHash –Path $_.FullName} $DestDocs = Get-ChildItem –Path C:\Documents2 | foreach  {Get-FileHash –Path $_.FullName}

就我的物理比较而言,我原本试图进行三方比较,所以下面的代码并不准确,但我已经得到了所有。

$folderA = 'C:\Users\USERNAME\Desktop\Folder A'
$folderB = 'C:\Users\USERNAME\Desktop\Folder B'
$folderC = 'C:\Users\USERNAME\Desktop\Folder C'
$AChild = Get-ChildItem $folderA -Recurse
$BChild = Get-ChildItem $folderB -Recurse
$CChild = Get-ChildItem $folderC -Recurse
Compare-Object -ReferenceObject $AChild -DifferenceObject $BChild, $CChildcode

在进一步研究之后,我发现使用3.0而不是2.0可能会更好,所以我可以更有效地使用-Dir。

有任何问题,请告诉我。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这是你想要的答案: 我想要做的是给出2个目录,A和B,并将A内的所有内容与B内的所有内容进行比较,如果B中有任何不在A中的文件或文件夹,则生成详细说明路径的输出文件到B中而不是A的项目。

 $folder1 = "C:\Users\USERNAME\Desktop\Folder A"
        $folder2 = "C:\Users\USERNAME\Desktop\Folder B"


        # Get all files under $folder1, filter out directories
        $firstFolder = Get-ChildItem -Recurse $folder1 | Where-Object { -not $_.PsIsContainer }

        $failedCount = 0
        $i = 0
        $totalCount = $firstFolder.Count
        $firstFolder | ForEach-Object {
            $i = $i + 1
            Write-Progress -Activity "Searching Files" -status "Searching File  $i of     $totalCount" -percentComplete ($i / $firstFolder.Count * 100)
            # Check if the file, from $folder1, exists with the same path under $folder2
            If ( Test-Path ( $_.FullName.Replace($folder1, $folder2) ) ) {
                # Compare the contents of the two files...
                If ( Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($folder1, $folder2) ) ) {
                    # List the paths of the files containing diffs
                    $fileSuffix = $_.FullName.TrimStart($folder1)
                    $failedCount = $failedCount + 1
                    Write-Host "$fileSuffix is on each server, but does not match"
                }
            }
            else
            {
                $fileSuffix = $_.FullName.TrimStart($folder1)
                $failedCount = $failedCount + 1
                Write-Host "$fileSuffix is only in folder 1"
$fileSuffix | Out-File "$env:userprofile\desktop\folder1.txt" -Append
            }
        }

        $secondFolder = Get-ChildItem -Recurse $folder2 | Where-Object { -not $_.PsIsContainer }

        $i = 0
        $totalCount = $secondFolder.Count
        $secondFolder | ForEach-Object {
            $i = $i + 1
            Write-Progress -Activity "Searching for files only on second folder" -status "Searching File  $i of $totalCount" -percentComplete ($i / $secondFolder.Count * 100)
            # Check if the file, from $folder2, exists with the same path under $folder1
            If (!(Test-Path($_.FullName.Replace($folder2, $folder1))))
            {
                $fileSuffix = $_.FullName.TrimStart($folder2)
                $failedCount = $failedCount + 1
                Write-Host "$fileSuffix is only in folder 2"
$fileSuffix | Out-File "$env:userprofile\desktop\folder2.txt" -Append
            }
        }