【问题标题】:Exclude file from powershell Get-ChildItem results从 powershell Get-ChildItem 结果中排除文件
【发布时间】:2018-09-18 08:14:17
【问题描述】:

要从一个目录中选择每个 AssemblyInfo.cs 减去一个,我运行一个 powershell 脚本:

Clear-Host
$SrcDir = "D:\Projects\Lambda\Sources"
Write-Host $SrcDir
cd $SrcDir
$ExcludeXYZCallStrategy = $SrcDir + "\Lambda.XYZ\XYZCallStrategy\Properties\AssemblyInfo.cs"
# 2. Select Files to be updated
Write-Host $ExcludeXYZCallStrategy
# both these syntaxes fail (tried one after the other not together ofc) :
$Assemblyfiles = Get-ChildItem -Path $SrcDir -Recurse -Include AssemblyInfo.cs -Exclude $ExcludeXYZCallStrategy
$Assemblyfiles = Get-ChildItem -Path $SrcDir -Recurse -Include AssemblyInfo.cs -Exclude D:\Projects\Lambda\Sources\Lambda.XYZ\XYZCallStrategy\Properties\AssemblyInfo.cs
Write-Host $Assemblyfiles
Write-Host "Files Count : " $Assemblyfiles.count

我尝试了不同的语法来强制 Get-ChildItem 排除该文件,但我失败了。

我尝试了这些答案无济于事:

doc,我没有找到任何可以解释为什么不排除该文件的内容。

【问题讨论】:

  • 作为一种解决方法,您可以这样做:| Where-Object {$_.FullName -ne $ExcludeXYZCallStrategy}
  • @guiwhatsthat 它有效!谢谢 !但为什么排除不起作用?
  • 这只是一个猜测,但我认为 exclude 参数只检查对象的 name 属性,您尝试使用 fullname 属性

标签: powershell get-childitem


【解决方案1】:

由于只有一个文件,你可以使用Where-Object

$Assemblyfiles = Get-ChildItem -Path $SrcDir -Recurse -Include AssemblyInfo.cs | Where-Object { $_.FullName -ne "D:\Projects\Lambda\Sources\Lambda.XYZ\XYZCallStrategy\Properties\AssemblyInfo.cs" }

【讨论】:

  • 有效!谢谢 !你能解释一下为什么排除没有吗?
  • Here's why 你的-exclude 参数不起作用!
猜你喜欢
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多