【问题标题】:How can I compare the content of 3 directories in powershell如何比较powershell中3个目录的内容
【发布时间】:2016-11-14 14:41:50
【问题描述】:

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

我想要做的是提供 2 个目录,A 和 B,并将 A 中的所有内容与 B 中的所有内容进行比较,如果 B 中有任何文件或文件夹不在 A 中,则生成一个输出文件,详细说明路径到 B 而不是 A 中的项目。从这里,我想使用项目列表,并说明这些路径中的任何项目是否包含与 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}

就我所拥有的物理比较而言,我最初是尝试进行 3 向比较,所以下面的代码并不准确,但我目前已经掌握了。

$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。

有任何问题,请告诉我。

非常感谢任何帮助。

【问题讨论】:

    标签: powershell directory comparison powershell-2.0 powershell-3.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
                }
            }
    

    【讨论】:

    • 谢谢!但是,这看起来不错,无论如何我可以将“(文件路径)仅在文件夹 1/2 中”输出到文件而不是仅显示在 powershell 中吗?
    • $fileSuffix | Out-File D:\lo.txt -Append add this line after write-host append do not support on powershell 2 see my solution here stackoverflow.com/questions/38242541/…
    • 修改后的答案追加在 powershell 2 上不支持在此处查看我的解决方案stackoverflow.com/questions/38242541/…
    • 太棒了!您能否就第二点提出建议?
    • 我不明白这是什么意思“如果我无法显示不同的文本,则生成另一个列表以显示不同之处或显示不同项目的文件路径。”
    猜你喜欢
    • 2013-05-04
    • 2012-10-01
    • 1970-01-01
    • 2012-12-03
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2015-04-17
    相关资源
    最近更新 更多