windows 获取存在于一个文件夹中但不存在于另一个文件夹中的文件列表- PowerShell

5jvtdoz2  于 2023-03-24  发布在  Windows
关注(0)|答案(3)|浏览(157)

给定两个目录(DirA和DirB),找到DirA中存在但DirB中不存在的文件列表的最有效方法是什么?
我尝试使用jdupes.exe --printunique --recurse -O来执行此操作,但是,如果DirA上存在重复的文件,则会排除符合上述标准的文件。
这些文件可能位于DirA和DirB的完全不同的子目录中,它们可能有不同的名称。因此,文件的内容是唯一持久的特征。

r7s23pms

r7s23pms1#

在windows中,我复制了dirB中的所有文件并在dirA中替换.所有替换的文件将被选中.不单击任何后藤文件夹的主菜单单击反转选择.它可能会有帮助.

vwkv1x7d

vwkv1x7d2#

如果你想比较文件的内容,你可以使用get-filehash来比较哈希值。
比如:

foreach ($folder1item in $folder1) {
    $f1hash = get-filehash $folder1item.fullname
    $f2hash = get-filehash ($folder2 | where name -eq ($folder1item.name)).fullname
    if ($f2hash.Hash -ne $f1hash.hash) {write-output "$($folder1item).fullname is different to $($folder2item).fullname"}
}
3pvhb19x

3pvhb19x3#

您可以使用compare-objectcmdlet比较两组对象,一组对象为“引用集”,另一组对象为“差异集”。
使用Compare-Objectcmdlet https://learn.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

相关问题