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

我试图find使用Windows Powershell的两个文件夹结构的内容的差异。 我已经使用下面的方法来确保文件名是相同的,但是这个方法并不告诉我文件的内容是否相同:

$firstFolder = Get-ChildItem -Recurse folder1 $secondFolder = Get-ChildItem -Recurse folder2 Compare-Object -ReferenceObject $firstFolder -DifferenceObject $secondFolder 

在这个ServerFault问题中描述的技术用于区分单个文件,但是这些文件夹包含数百个各种深度的文件。

解决scheme并不一定需要告诉我具体在文件中是什么不同 – 只是他们是。 我对元数据的差异不感兴趣,比如我已经知道的date不同。

如果你想把比较放到一个循环中,我会采取以下的方法:

 $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) } } } 

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

我已经采取了jscott的答案扩大它输出的文件是在一个而不是其他的那些谁是在这种types的function的兴趣。 请注意,它也显示了进步,因为我很难看到,由于巨大的文件夹没有太多的差异。 看起来好像脚本挂在了我身上。 下面是该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" } } 

你只是围绕已经回答了这个问题的链接问题打包一个循环,然后遍历目录树,比较每个具有相同名字的文件。

/编辑:如果这实际上是你的问题,那么对于你来说,这似乎是一个普通的贡献者。 你在问一个编程问题。 我知道你正在做一个系统pipe理员types的目的,在这种情况下,我会告诉你使用像WinDiff这样的专用工具。