我正在尝试使用 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)
您只需围绕已回答此问题的链接问题的正确答案进行循环,然后遍历目录树比较每个具有相同名称的文件。
/编辑:如果这确实是您的问题,那么它更适合SO,您似乎是定期贡献者。你问的是一个编程问题。我知道您这样做是为了系统管理员类型的目的,在这种情况下,我会告诉您使用 WinDiff 等专用工具。
归档时间: |
|
查看次数: |
28108 次 |
最近记录: |