【问题标题】:Get list of files that exist in one folder but not another - PowerShell获取存在于一个文件夹中但不存在于另一个文件夹中的文件列表 - PowerShell
【发布时间】:2022-01-09 11:07:07
【问题描述】:

给定 2 个目录(DirA 和 DirB),找到在 DirA 中存在但在 DirB 中不存在的文件列表的最有效方法是什么?

我尝试使用 jdupes.exe --printunique --recurse -O 来执行此操作,但是,如果在 DirA 上存在所述文件的重复项,则会排除满足上述条件的文件。

这些文件可能位于 DirA 和 DirB 的完全不同的子目录中,并且它们可能具有不同的名称。所以文件的内容是唯一持久的特征。

【问题讨论】:

  • 请与我们分享您使用powershell解决此问题的尝试
  • “所以文件的内容是唯一持久的特征”.. 看起来您需要使用文件的完整路径和名称创建列表,并结合文件的哈希码。然后比较这些哈希值以确定两个或多个文件是否相同。应该是一个冗长的练习..
  • 是的......这应该是一个漫长的过程......我想知道是否有一个我不知道的工具或某些功能可能会使问题变得更容易。

标签: windows powershell duplicates


【解决方案1】:

您可以使用 compare-object cmdlet 来比较两组对象。一组对象是“reference set”,另一组是“difference set”。

使用 Compare-Object cmdlet https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.2

比较对象的过程

Step1 - Folders To Be Searched
Step2 - Search Both Folders For Files To Be Compared
Step3 - See All Compare Output
Step4 - See Only Difference in reference objects

** Step4a - '=>' - Difference in destination object.
** Step4b - '<=' - Difference in reference (source) object.
** Step4c - '==' - When the source and destination objects are equal.

比较两个文件夹中对象的示例脚本

## Step 1 - Folders To Be Searched
$folder1 = "C:\Temp"
$folder2 = "C:\Test"

## Step 2 - Search Both Folders For Files To Be Compared
$List1 = gci $folder1 -Recurse | Select Name
$List2 = gci $folder2 -Recurse | Select Name

## Step 3 - See All Compare Output
$Compare = Compare-Object -ReferenceObject $List1 -DifferenceObject $List2 -property name -passthru -IncludeEqual


## Step 4 See Only Difference in reference objects
##=> - Difference in destination object.
##<= - Difference in reference (source) object.
##== - When the source and destination objects are equal.

$DifferenceInReference = (Compare-Object -ReferenceObject $List1 -DifferenceObject $List2 | Where SideIndicator -eq "<=")
$DifferenceInReference
$DifferenceInDestination = (Compare-Object -ReferenceObject $List1 -DifferenceObject $List2 | Where SideIndicator -eq "=>")
$DifferenceInDestination
$EqualInBoth = (Compare-Object -ReferenceObject $List1 -DifferenceObject $List2 | Where SideIndicator -eq "==")
$EqualInBoth

【讨论】:

  • 这只比较文件名。 OP 想要比较 content
  • @Theo... “给定 2 个目录(DirA 和 DirB),查找 DirA 中存在但 DirB 中不存在的文件列表的最有效方法是什么?”你的基本要求太过分了!
  • 我不这么认为..这是来自 OP 的要求:"这些文件可能位于 DirA 和 DirB 的完全不同的子目录中,并且它们可能具有不同的名称。所以 文件的内容是唯一持久的特征。"
  • 我不是在和你辩论。只是说你的代码只比较文件名,不幸的是这不是这里的问题..顺便说一句。是什么让您认为这些文件都是基于文本的,所以字符串匹配会有所帮助?
  • @Theo 它是一种建议方法,直到提供清晰为止;o)
猜你喜欢
  • 2017-02-09
  • 2011-01-27
  • 2018-12-14
  • 2012-11-11
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
相关资源
最近更新 更多