powershell - 查找并递归

标签 powershell powershell-2.0

我有一个文件夹结构d:\domains\<domain>\folder1\folder2\folderx
可能有20个<domain>文件夹,它们下面的文件夹级别不同。

我想在所有文件夹中搜索.php文件,然后仅在退出时打印唯一的<domain>文件夹。

因此,例如,如果在

  • d:\ domains \ domain1.com \ test \ test
  • d:\ domains \ domain2.com \ test \ test
  • d:\ domains \ domain2.com \ test \ help

  • 我只想打印domain1.com,domain2.com。它需要在PowerShell v2中工作。

    我有以下内容,但它仅显示第一个域?
    Get-ChildItem -Path @(Get-ChildItem -Path D:\domains | Where-Object {$_.PSIsContainer})[1].FullName -Recurse *.php |
        Select-Object -ExpandProperty FullName
    

    最佳答案

    枚举域文件夹,然后筛选其中包含.php文件的文件夹。

    Get-ChildItem 'D:\domains' | Where-Object {
        $_.PSIsContainer -and
        (Get-ChildItem $_.FullName -Filter '*.php' -Recurse)
    }
    

    如果您具有PowerShell v3或更高版本,则可以使用-Directory开关而不是检查$_.PSIsContainer:
    Get-ChildItem 'D:\domains' -Directory | Where-Object {
        Get-ChildItem $_.FullName -Filter '*.php' -Recurse
    }
    

    如果只需要文件夹/域名,请从输出中选择Name属性:
    ... | Select-Object -Expand Name
    

    关于powershell - 查找并递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48539759/

    相关文章:

    powershell - Get-WindowsFeature 的替代方案

    powershell - 即使 PS 脚本成功执行,Jenkins 作业也失败

    function - 将功能转发到新的 powershell session 还是使用 Invoke-command?

    Powershell 速度 : How to speed up ForEach-Object MD5/hash check

    azure - Get-AzStorageBlob - 特定文件夹

    powershell - 使用Powershell对带有标题的CSV目录中的列求和

    powershell - CSV文件按col1分组并显示col2的最大值

    c# - 由于未加密的流量,PowerShell 远程命令失败

    powershell - 获取内容 powershell commandlet 未返回

    powershell - Python 的 doctest 模块是否有 PowerShell 等价物?