【问题标题】:What is the equivalent of »dir /s /b *foo*.docx« in powershellpowershell 中 »dir /s /b *foo*.docx« 的等价物是什么
【发布时间】:2014-12-09 11:18:53
【问题描述】:

我正在寻找 cmd.exe's 的 powershell 等效项

dir /s /b *foo*.docx

我想为此创建一个别名(或 scriptlet?)。

我找到了How do I do 'dir /s /b' in PowerShell?,但使用这种方法我似乎无法传递*foo*.docx 的参数。

【问题讨论】:

  • gci -recurse -filter *foo*.docx | select -expandproperty fullname
  • @arco444 这一定是答案,你应该在别人之前写出来。我测试了-include 只是为了看看,-filter 更快。

标签: powershell cmd


【解决方案1】:

正如我在上面的评论中所说,您正在寻找:

gci -recurse -filter *foo*.docx | select -expandproperty fullname

gciGet-ChildItem 将返回与您指定的过滤器匹配的文件系统对象列表。每个对象都会附带大量信息,但由于您只对路径感兴趣,因此您可以通过管道将fullname 属性通过管道传递到select,如图所示。 -expandproperty 标志将确保结果以字符串数组而不是对象列表的形式返回。

【讨论】:

  • 或者,如果您有 Powershell 3 或更高版本,只需执行(dir -recurse -filter *foo*.docx).fullname(其中dirgciGet-ChildItemls 当然可以在任何版本中互换,只需使用您的最喜欢的)。
【解决方案2】:
 Get-ChildItem -Recurse -Filter *foo*.docx | Select-Object FullName | Sort-Object Length

【讨论】:

  • Arco 的这个答案是正确的,虽然他还没有回答,但我认为给他更多时间是一种很好的形式。此外,这里不起作用的一件事是您返回一个具有单个属性的对象数组,而不是一个字符串数组,这是 OP sort of 中的 cmd 所做的。
  • 只选择 fullname 属性,然后按长度排序(不再存在)不会很好。
猜你喜欢
  • 1970-01-01
  • 2016-07-25
  • 2011-07-18
  • 1970-01-01
  • 2011-05-13
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多