如何在 Windows Powershell 中区分两个文件夹?

Dav*_*ith 14 powershell diff

我正在尝试使用 Windows Powershell 查找两个文件夹结构的内容差异。我使用了以下方法来确保文件名相同,但是此方法不会告诉我文件的内容是否相同:

$firstFolder = Get-ChildItem -Recurse folder1
$secondFolder = Get-ChildItem -Recurse folder2
Compare-Object -ReferenceObject $firstFolder -DifferenceObject $secondFolder
Run Code Online (Sandbox Code Playgroud)

此 ServerFault 问题中描述的技术适用于比较单个文件,但这些文件夹包含数百个不同深度的文件。

解决方案不一定需要告诉我文件中的具体内容不同 - 只是它们是不同的。我对元数据的差异不感兴趣,例如日期,我已经知道它们是不同的。

jsc*_*ott 16

如果要将比较包装到循环中,我将采用以下方法:

$folder1 = "C:\Users\jscott"
$folder2 = "C:\Users\public"

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

$firstFolder | ForEach-Object {

    # 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
            $_.FullName
            $_.FullName.Replace($folder1, $folder2)

        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

请注意,这将忽略 $folder1和 中都不存在的文件$folder2


小智 5

我已经对 jscott 的回答进行了扩展,以输出存在于一个而不是另一个中的文件,供那些对这种类型的功能感兴趣的人使用。请注意,它也显示了取得的进展,因为我很难看到那些巨大的文件夹,但差异并不大。看起来剧本是挂在我身上的。这是用于此的powershell代码:

$folder1 = "C:\Folder1"
$folder2 = "C:\Folder2"

# 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"
    }
}

$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"
    }
}
Run Code Online (Sandbox Code Playgroud)


mfi*_*nni 1

您只需围绕已回答此问题的链接问题的正确答案进行循环,然后遍历目录树比较每个具有相同名称的文件。

/编辑:如果这确实是您的问题,那么它更适合SO,您似乎是定期贡献者。你问的是一个编程问题。我知道您这样做是为了系统管理员类型的目的,在这种情况下,我会告诉您使用 WinDiff 等专用工具。

  • 我想你是在床的另一边醒来的。我不是普通的 Powershell 用户。我在我的问题中展示了我当前正在尝试的一种技术,以及一个问题的链接,其中包含对我的问题有更多帮助的信息。我不知道如何结合这两种技术,这是我的问题,也是我问这个问题的原因。这也是我使用谷歌搜索无法回答的问题。如果您不会提供帮助,请考虑删除您的答案。 (3认同)
  • @voretaq7 我认为你们误解了我作为 PowerShell 用户的意思。我进行了谷歌搜索,尝试了该技术,但没有成功。我试图解释我在上面的问题中所处的位置。您链接到的问题适用于一组文件。我有两组需要逐个比较。我真的不想在这里偷懒。我知道如何循环,也知道如何比较。如何循环并比较两个集合? (3认同)
  • 好的,该网站不适合回答“给我代码”类型的问题。如果您需要开始学习如何在 Powershell 中进行循环,请购买书籍或查找在线教程;有许多。 (2认同)
  • @BigDave [Google 上“PowerShell 循环文件”的字面意思*第一个结果*](http://stackoverflow.com/questions/1523043/how-to-loop-through-files-and-rename-using-powershell) 。现在来吧 - 你的“一点点”努力? (2认同)