Tim*_*phy 10 directory powershell get-childitem
使用PowerShell我可以使用以下命令获取目录:
Get-ChildItem -Path $path -Include "obj" -Recurse | `
Where-Object { $_.PSIsContainer }
Run Code Online (Sandbox Code Playgroud)
我更喜欢编写一个函数,因此命令更具可读性.例如:
Get-Directories -Path "Projects" -Include "obj" -Recurse
Run Code Online (Sandbox Code Playgroud)
除了-Recurse
优雅处理外,以下功能完全正确:
Function Get-Directories([string] $path, [string] $include, [boolean] $recurse)
{
if ($recurse)
{
Get-ChildItem -Path $path -Include $include -Recurse | `
Where-Object { $_.PSIsContainer }
}
else
{
Get-ChildItem -Path $path -Include $include | `
Where-Object { $_.PSIsContainer }
}
}
Run Code Online (Sandbox Code Playgroud)
如何if
从我的Get-Directories函数中删除该语句,或者这是一种更好的方法吗?
x0n*_*x0n 13
试试这个:
# nouns should be singular unless results are guaranteed to be plural.
# arguments have been changed to match cmdlet parameter types
Function Get-Directory([string[]]$path, [string[]]$include, [switch]$recurse)
{
Get-ChildItem -Path $path -Include $include -Recurse:$recurse | `
Where-Object { $_.PSIsContainer }
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为-Recurse:$ false同样没有-Recurse.
归档时间: |
|
查看次数: |
13241 次 |
最近记录: |