I have hashset1 that has items as filenames from one file share and hashset2 that has items as filenames from another file share. I want to find all the file names that are common in both and put them in a third hashset viz $setDiff.There can’t be no duplicates. The condition should work even if one of the set is empty, i think it would work if you do the diff is my expectation.
I find that its not working sometimes, and i get 0, although i see there are files that match in both sets.
I have a sample screenshot of the file names of the 2 sets that i am trying to compare.
$setOrig = New-Object System.Collections.Generic.HashSet[System.String]
$setOrigNames = New-Object System.Collections.Generic.HashSet[System.String]
$setArchived = New-Object System.Collections.Generic.HashSet[System.String]
$setArchivedNames = New-Object ‘System.Collections.Generic.HashSet[System.String]’
$setDiff = New-Object System.Collections.Generic.HashSet[System.String]
$origNamePathPair = New-Object ‘System.Collections.Generic.Dictionary[string,string]’
$archNamePathPair = New-Object ‘System.Collections.Generic.Dictionary[string,string]’
$setOrig = Get-ChildItem -Path $_.FullName -Recurse -Include("*.png")$setArchived |
ForEach-Object { $archNamePathPair[$_.BaseName] = $setArchived[$i], $i++ }
$idx = 0
$setOrig | ForEach-Object { $origNamePathPair[$_.BaseName] = $setOrig[$idx], $idx++
}
$setOrigNames = $setOrig | ForEach-Object {$_.BaseName}
$setArchivedNames = $setArchived | ForEach-Object {$_.BaseName}
$hash1 = [System.Collections.Generic.HashSet[string]]::new(
[string[]] ($setOrigNames),
[System.StringComparer]::InvariantCulture)
$hash2 = [System.Collections.Generic.HashSet[string]]::new(
[string[]] ($setArchivedNames),
[System.StringComparer]::InvariantCulture)
$setDiff = @(
$hash2.Where{ $hashset1.Contains($_) }
)
enter code here
4