如何从Get-ChildItem列表中排除导入的目录列表?

上传时,我们的在线备份软件正在跳过文件和目录。 这似乎是随机的,但我想看看是否可以将select中的文件列表与云中的文件列表进行比较以查找模式。

云中的项目以.csv格式提供,但是在排除其他目录时,我无法在备份select中生成文件列表。 这是我迄今为止。

$colSelection = get-content "c:\scripts\selection.txt" $colExclusion = get-content "c:\scripts\exclusion.txt" foreach ($folder in $colSelection) { $colItems = (Get-ChildItem $folder -recurse -force | where-object {(-not $_.PSIsContainer) -and ($_.FullName -notlike $colExclusion)}) foreach ($item in $colItems) { Add-Content -Path c:\scripts\testlist.txt -Value $item.FullName } } 

.txt文件是目录的列表。 最好的办法做到这一点似乎是使用正则expression式,但我不知道我可以从一个.txt文件dynamic地创buildexpression式。

由于Get-Content默认返回一个string集合(或者数组 ,如果你愿意的话),所以你要比较$_.FullNamestring和string集合。

您可以使用-notcontains查看整个数组的父目录:

 $childItems = Get-ChildItem $folder -Recurse -Force $childItems | Where-Object {$colExclusion -notcontains $_.Directory.FullName -and (-not $_.PSIsContainer)} 

或者您可以使用Where-Objectfilter中的ForEach-Object调用,将$colItems中的每个项目与每个path的开头进行比较:

 $childItems | Where-Object {$( $path = $_.FullName $colItems | ForEach-Object { if($_ -notlike "$path*"){ return $true } } )} 

TesselatingHeckler是正确的。

我可以随时创buildexpression式,下面的工作。 我确实需要转义目录列表中的所有斜杠。

 $colSelection = get-content "c:\scripts\selection.txt" #$colExclusion = get-content "c:\scripts\exclusion.txt" $colExclusion = "($((get-content "c:\scripts\exclusion.txt") -join '|' ))" foreach ($folder in $colSelection) { $colItems = (Get-ChildItem $folder -recurse -force | where-object {$_.FullName -notmatch $colExclusion}) foreach ($item in $colItems) { Add-Content -Path c:\scripts\testlist.txt -Value $item.FullName } }