从 Arraylist Powershell 中删除对象
Remove Object from Arraylist Powershell
我想删除 Arraylist 中的对象。由于我是 Powershell 的新手,我发现自己遇到了很多麻烦。
更具体地说,我想删除第一个 Arraylist 中的对象,如果该对象与第二个 Arraylist 中的对象相同,则应该将其删除。
这是我到目前为止尝试过的方法。作为参考,我尝试使用此页面:https://www.sapien.com/blog/2014/11/18/removing-objects-from-arrays-in-powershell/
[System.Collections.ArrayList]$everyExistingFolder = (Get-ChildItem -Path "$PSScriptRoot\test" | foreach { $_.Name })
[System.Collections.ArrayList]$everyFolderWichShouldExist = (Get-Content "$PSScriptRoot\directoriesADExport.txt")
$output = ForEach ($element in $everyExistingFolder)
{
if($element -eq $everyFolderWichShouldExist){
$everyExistingFolder.Remove($element)
}
}
Out-File $output
有这样的可能吗,还是我试错了方法。
很想听听任何提示或建议。
你很接近,你只需要替换 -Eq
运算符,这意味着等于,对于数组运算符之一,如 -Contains
或 -In
[System.Collections.ArrayList]$everyExistingFolder = (Get-ChildItem -Path "$PSScriptRoot\test" | foreach { $_.Name })
[System.Collections.ArrayList]$everyFolderWichShouldExist = (Get-Content "$PSScriptRoot\directoriesADExport.txt")
$output = ForEach ($element in $everyExistingFolder)
{
if($everyFolderWichShouldExist -contains $element){
"need to remove $($element) from the array `everyExistingFolder"
$everyExistingFolder.Remove($element)
}
}
Out-File $output
我假设您这样做是为了获取需要创建的文件夹列表。尽管 从 ArrayList
中删除是完全可能的,但您在这种情况下 尝试做的不是 。据我所知,您不能在枚举集合时修改它。
您没有收到任何错误的原因是您使用了相等运算符 -eq
,如果您需要在集合中查找元素,则需要使用 containment operator相反(-in
或 -contains
在这种情况下)。
如果代码是正确的,下面是重现异常的步骤:
using namespace System.IO
using namespace System.Collections
$foldersExisting = 'Folder1', 'Folder2', 'Folder3', 'Folder4', 'Folder5'
$foldersFromFile = 'Folder2', 'Folder4', 'Folder6', 'Folder7'
[ArrayList]$listExisting = [FileInfo[]]$foldersExisting
[ArrayList]$listFromFile = [FileInfo[]]$foldersFromFile
foreach($folder in $listExisting)
{
if($folder.Name -in $listFromFile.Name)
{
$listExisting.Remove($folder)
}
}
以上代码会抛出以下异常:
Collection was modified; enumeration operation may not execute.
至于你想要完成的事情($everyExistingFolder
应该包含 不是 在 $everyFolderWichShouldExist
上找到的那些元素,你应该过滤基于第二个数组元素的第一个数组:
PS/> $foldersExisting.where({ $_ -notin $foldersFromFile })
Folder1
Folder3
Folder5
如果您确实需要从第一个集合中删除项目,您可以这样做,首先过滤要删除的元素,然后循环遍历它们。结果与上面显示的过滤技术相同。
foreach($folder in $listExisting.Where({ $_.Name -in $listFromFile.Name }))
{
$listExisting.Remove($folder)
}
PS /> $listExisting
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
12/31/1600 20:07 Folder1 ->
12/31/1600 20:07 Folder3 ->
12/31/1600 20:07 Folder5 ->
我想删除 Arraylist 中的对象。由于我是 Powershell 的新手,我发现自己遇到了很多麻烦。
更具体地说,我想删除第一个 Arraylist 中的对象,如果该对象与第二个 Arraylist 中的对象相同,则应该将其删除。
这是我到目前为止尝试过的方法。作为参考,我尝试使用此页面:https://www.sapien.com/blog/2014/11/18/removing-objects-from-arrays-in-powershell/
[System.Collections.ArrayList]$everyExistingFolder = (Get-ChildItem -Path "$PSScriptRoot\test" | foreach { $_.Name })
[System.Collections.ArrayList]$everyFolderWichShouldExist = (Get-Content "$PSScriptRoot\directoriesADExport.txt")
$output = ForEach ($element in $everyExistingFolder)
{
if($element -eq $everyFolderWichShouldExist){
$everyExistingFolder.Remove($element)
}
}
Out-File $output
有这样的可能吗,还是我试错了方法。
很想听听任何提示或建议。
你很接近,你只需要替换 -Eq
运算符,这意味着等于,对于数组运算符之一,如 -Contains
或 -In
[System.Collections.ArrayList]$everyExistingFolder = (Get-ChildItem -Path "$PSScriptRoot\test" | foreach { $_.Name })
[System.Collections.ArrayList]$everyFolderWichShouldExist = (Get-Content "$PSScriptRoot\directoriesADExport.txt")
$output = ForEach ($element in $everyExistingFolder)
{
if($everyFolderWichShouldExist -contains $element){
"need to remove $($element) from the array `everyExistingFolder"
$everyExistingFolder.Remove($element)
}
}
Out-File $output
我假设您这样做是为了获取需要创建的文件夹列表。尽管 从 ArrayList
中删除是完全可能的,但您在这种情况下 尝试做的不是 。据我所知,您不能在枚举集合时修改它。
您没有收到任何错误的原因是您使用了相等运算符 -eq
,如果您需要在集合中查找元素,则需要使用 containment operator相反(-in
或 -contains
在这种情况下)。
如果代码是正确的,下面是重现异常的步骤:
using namespace System.IO
using namespace System.Collections
$foldersExisting = 'Folder1', 'Folder2', 'Folder3', 'Folder4', 'Folder5'
$foldersFromFile = 'Folder2', 'Folder4', 'Folder6', 'Folder7'
[ArrayList]$listExisting = [FileInfo[]]$foldersExisting
[ArrayList]$listFromFile = [FileInfo[]]$foldersFromFile
foreach($folder in $listExisting)
{
if($folder.Name -in $listFromFile.Name)
{
$listExisting.Remove($folder)
}
}
以上代码会抛出以下异常:
Collection was modified; enumeration operation may not execute.
至于你想要完成的事情($everyExistingFolder
应该包含 不是 在 $everyFolderWichShouldExist
上找到的那些元素,你应该过滤基于第二个数组元素的第一个数组:
PS/> $foldersExisting.where({ $_ -notin $foldersFromFile })
Folder1
Folder3
Folder5
如果您确实需要从第一个集合中删除项目,您可以这样做,首先过滤要删除的元素,然后循环遍历它们。结果与上面显示的过滤技术相同。
foreach($folder in $listExisting.Where({ $_.Name -in $listFromFile.Name }))
{
$listExisting.Remove($folder)
}
PS /> $listExisting
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
12/31/1600 20:07 Folder1 ->
12/31/1600 20:07 Folder3 ->
12/31/1600 20:07 Folder5 ->