Answers:
为什么你认为你的命令不优雅?如果您只是处理更简洁的代码,那么在Code Review上可能会更好。
顺便说一句,我认为这个命令不起作用。
如果要使用-like
,请添加通配符:
gci | ? {$_.Name -like "*foo*" -and $_.Name -like "*bar*"}
如果您不想使用通配符,请使用-match
:
gci | ? {$_.Name -match "foo" -and $_.Name -match "bar"}
皮肤猫的方法有很多种
gci *foo* | ? {$_.Name -in (gci *bar*).Name}
我认为更优雅的代码是主观的,真的取决于你的编码风格。我个人更喜欢你的代码行,因为它根本不是神秘的,它的功能相当紧凑。
$_.Name
)。我也希望有一种不需要的手段-and
。最好是能够对多个条件进行排序,例如"*foo*","*bar*"
,结果都是匹配所有条件的对象。
我如何理解这个问题的标题答案如下:
'*.txt','*.log' | ForEach-Object { Get-ChildItem $_ } # 'pure' Powershell
where.exe /r . *.txt *.log # external command
但是,为了获得名称中包含'foo' 和 'bar'的文件(我认为是一种模式),一些明显的解决方案可能是:
Get-ChildItem *foo*bar* # if the order is given
Get-ChildItem `
| Where-Object Name -match 'foo.*bar|bar.*foo' # if not
in
Operater仅适用于PS3.0 +