简体   繁体   中英

Get list of files that exist in one folder but not another - PowerShell

Given 2 directories (DirA and DirB) what's the most efficient way to find a list of files which exist in DirA but do not exist in DirB?

I tried to do this using the jdupes.exe --printunique --recurse -O, however, this has the side-effect of excluding a file that meet the above criteria if there exist duplicates of said file on DirA.

The files may be in completely different subdirectories of DirA and DirB and they may have different names. So the content of the file is the only durable characteristic.

You can use the compare-object cmdlet to compares two sets of objects. One set of objects is the " reference set ," and the other set is the " difference set ."

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

Process To Compare Objects

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.

Sample Script To Compare Objects In Two Folders

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

in windows, i copied all files in dirB and replaced in dirA. all the files replaced will be selected. without clicking anywhere goto folder's home menu click invert selection. it may help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM