31 powershell
我正在尝试获取目录中所有 XSL 和 XSLT 文件的列表。
dir -recurse -filter *.xsl,*.xslt -name
Run Code Online (Sandbox Code Playgroud)
但出现以下错误:
Get-ChildItem:无法将“System.Object[]”转换为参数“Filter”所需的类型“System.String”。不支持指定的方法。
dir -recurse -filter *.xsl -filter *.xslt -name
Run Code Online (Sandbox Code Playgroud)
但是得到了这个错误:
Get-ChildItem:无法绑定参数,因为多次指定了参数“过滤器”。要为可以接受多个值的参数提供多个值,请使用数组语法。例如,“-parameter value1,value2,value3”。
我可以用一个命令列出两个文件扩展名吗?
EBG*_*een 36
dir .\* -include ('*.xsl', '*.xslt') -recurse
Run Code Online (Sandbox Code Playgroud)
cry*_*man 18
我不知道为什么,但这个似乎要快得多:
dir .\ -recurse | where {$_.extension -in ".xsl",".xslt"}
Run Code Online (Sandbox Code Playgroud)
Kei*_*ler 13
“过滤器”*.xsl也*.xslt可以是路径参数的一部分。路径可以采用字符串数组:
Set-Location c:\topLevel
gci *.xsl, *.xslt -Recurse
Run Code Online (Sandbox Code Playgroud)
如果要指定当前位置以外的路径,或搜索多个位置,请创建几个字符串数组并用于Join-Path创建路径数组:
PS C:\> $paths = 'c:\topLevel' , 'd:\topLevel'
PS C:\> $filters = '*.xsl' , '*.xslt'
PS C:\> $filters | ForEach-Object { Join-Path $paths $_ }
c:\topLevel\*.xsl
d:\topLevel\*.xsl
c:\topLevel\*.xslt
d:\topLevel\*.xslt
PS C:\>
Run Code Online (Sandbox Code Playgroud)
该输出可以通过管道传输到 Get-ChildItem:
$paths = 'c:\topLevel' , 'd:\topLevel'
$filters = '*.xsl' , '*.xslt'
$filters | % { Join-Path $paths $_ } | gci -name
Run Code Online (Sandbox Code Playgroud)
如果您不需要 FileInfo 对象的附加属性,而只需要完全限定路径的列表,Resolve-Path速度会更快:
$paths = 'c:\topLevel' , 'd:\topLevel'
$filters = '*.xsl' , '*.xslt'
$filters | % { Join-Path $paths $_ } | Resolve-Path
Run Code Online (Sandbox Code Playgroud)
我的快速测试:
PS C:\>Measure-Command { $filters | % { Join-Path $path $_ } | gci | select -expand FullName }
...
TotalMilliseconds : 31.2376
PS C:\>Measure-Command { $filters | % { Join-Path $path $_ } | Resolve-Path }
...
TotalMilliseconds : 12.2536
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62370 次 |
| 最近记录: |