Powershell 将两个列表与 foreach 进行比较
Powershell comparing two List with foreach
我正在尝试比较使用 Get-ChildItem
cmdlet 获得的两个列表。
我阅读了 Compare-Object
cmdlet,但在这种情况下我更喜欢使用 foreach,因为它有助于访问 $file
变量。
$StartingFolderPath = "C:\---\StartingFolder"
$EndingFolderPath = "C:\---\EndingFolder"
$AllStartingFiles = Get-ChildItem $StartingFolderPath
$AllEndingFiles = Get-ChildItem $EndingFolderPath
Write-Host "First folder content:"$AllStartingFiles
Write-Host "Second folder content:" $AllEndingFiles
foreach($file in $AllEndingFiles){
write-Host "Element :" $file.Name
$result = $AllStartingFiles.Contains($file.Name)
write-host $result
if($AllStartingFiles.Contains($file.Name)){
Write-Host "You are here"
Write-Host $file.Name
}
}
但是我好像无法通过if控制,if($AllStartingFiles.Contains($file.Name))
which returns false.
输出
First folder content: 1_one.txt 2_two.txt 3_three.txt 5_five.txt
Second folder content: 1_one.txt 2_two.txt 3_three.txt 4_four.txt 5_five.txt
6_six.txt
Element : 1_one.txt
False
Element : 2_two.txt
False
Element : 3_three.txt
False
Element : 4_four.txt
False
Element : 5_five.txt
False
Element : 6_six.txt
False
我也尝试过使用 -Contains
运算符,但没有成功。
您在
期间没有解决 Name
属性
$result = $AllStartingFiles.Contains($file.Name)
正确的方法是:
$result = $AllStartingFiles.Name.Contains($file.Name)
如果只是不想使用Compare-Object
,那就更不用说了,因为你害怕你会丢失文件属性,事实并非如此。
Compare-Object -ReferenceObject $AllEndingFiles -DifferenceObject $AllStartingFiles -IncludeEqual -ExcludeDifferent | Select-Object -ExpandProperty InputObject
这将为您提供出现在两个列表中的所有文件及其属性。
我不知道不使用比较对象的原因:
> tree /F
├───first
│ 1_one.txt
│ 2_two.txt
│ 3_three.txt
│ 5_five.txt
│
└───second
1_one.txt
2_two.txt
3_three.txt
4_four.txt
5_five.txt
> compare (gci .\first\) (gci .\second\)
InputObject SideIndicator
----------- -------------
4_four.txt =>
我正在尝试比较使用 Get-ChildItem
cmdlet 获得的两个列表。
我阅读了 Compare-Object
cmdlet,但在这种情况下我更喜欢使用 foreach,因为它有助于访问 $file
变量。
$StartingFolderPath = "C:\---\StartingFolder"
$EndingFolderPath = "C:\---\EndingFolder"
$AllStartingFiles = Get-ChildItem $StartingFolderPath
$AllEndingFiles = Get-ChildItem $EndingFolderPath
Write-Host "First folder content:"$AllStartingFiles
Write-Host "Second folder content:" $AllEndingFiles
foreach($file in $AllEndingFiles){
write-Host "Element :" $file.Name
$result = $AllStartingFiles.Contains($file.Name)
write-host $result
if($AllStartingFiles.Contains($file.Name)){
Write-Host "You are here"
Write-Host $file.Name
}
}
但是我好像无法通过if控制,if($AllStartingFiles.Contains($file.Name))
which returns false.
输出
First folder content: 1_one.txt 2_two.txt 3_three.txt 5_five.txt
Second folder content: 1_one.txt 2_two.txt 3_three.txt 4_four.txt 5_five.txt
6_six.txt
Element : 1_one.txt
False
Element : 2_two.txt
False
Element : 3_three.txt
False
Element : 4_four.txt
False
Element : 5_five.txt
False
Element : 6_six.txt
False
我也尝试过使用 -Contains
运算符,但没有成功。
您在
期间没有解决Name
属性
$result = $AllStartingFiles.Contains($file.Name)
正确的方法是:
$result = $AllStartingFiles.Name.Contains($file.Name)
如果只是不想使用Compare-Object
,那就更不用说了,因为你害怕你会丢失文件属性,事实并非如此。
Compare-Object -ReferenceObject $AllEndingFiles -DifferenceObject $AllStartingFiles -IncludeEqual -ExcludeDifferent | Select-Object -ExpandProperty InputObject
这将为您提供出现在两个列表中的所有文件及其属性。
我不知道不使用比较对象的原因:
> tree /F
├───first
│ 1_one.txt
│ 2_two.txt
│ 3_three.txt
│ 5_five.txt
│
└───second
1_one.txt
2_two.txt
3_three.txt
4_four.txt
5_five.txt
> compare (gci .\first\) (gci .\second\)
InputObject SideIndicator
----------- -------------
4_four.txt =>