I have three files in test1
folder:
I have three files in test2
folder:
Common between these two folders is file2
.
I want to subtract common files between these two folders and get only unique files from test1
folder.
My expected output is:
file1.txt
file3.txt
I tried using:
cls
$parent='D:\test1'
$child='D:\test2'
$final = [System.Collections.ArrayList]@()
$parentarrays=Get-ChildItem $parent
$childarrays=Get-ChildItem $child
foreach ($p1 in $parentarrays) {
foreach ($p2 in $childarrays){
if($p1 -notcontains $p2){
$final.add($p1)
}
}
}
Write-Host $final
But I am getting output:
file1.txt.txt file1.txt.txt file1.txt.txt file2.txt.txt file2.txt.txt file2.txt.txt file3.txt.txt file3.txt.txt file3.txt.txt
CodePudding user response:
Get a list of filenames in folder 2 and do the same for folder1. Use a Where-Object
clause to filter out any filename that is also in the reference list:
$filesInFolder2 = (Get-ChildItem -Path 'D:\Test2' -File).Name
(Get-ChildItem -Path 'D:\Test1' -File).Name | Where-Object { $filesInFolder2 -notcontains $_ }
Output:
file1.txt
file3.txt