使用powershell进行文件系统查询

使用powershell进行文件系统查询,powershell,Powershell,我有一个power shell高级问题,我没有运气就搜索了答案/示例 查询: 我想获得第N个最新的文件夹文件(没有子文件夹),条件是该文件夹包含foo.xml 范例 输入:根文件夹=Z z:…\A\B\foo.xml z:…..\A\B\bar.txt z:…\A\B\C\koo.txt z:…..\A\foo.xml z:…\A\abc.doc z:…..\D\xyz.xls 查询的输出 z:…\A\B\foo.xml z:…..\A\B\bar.txt z:…..\A\foo.xml z:…

我有一个power shell高级问题,我没有运气就搜索了答案/示例

查询: 我想获得第N个最新的文件夹文件(没有子文件夹),条件是该文件夹包含foo.xml

范例 输入:根文件夹=Z

z:…\A\B\foo.xml

z:…..\A\B\bar.txt

z:…\A\B\C\koo.txt

z:…..\A\foo.xml

z:…\A\abc.doc

z:…..\D\xyz.xls

查询的输出

z:…\A\B\foo.xml

z:…..\A\B\bar.txt

z:…..\A\foo.xml

z:…\A\abc.doc


我想使用powershell来提高性能(高级语言中的循环太慢)。

我认为最简单的方法是:

  • 查找整个树中的所有“foo.xml”
  • 在父目录中查找最新文件
  • 输出其
    FullName
    (其路径)属性
  • 这应该做到:

    funtion Get-NewestFooFileSibling {
        param(
            [string]$RootPath = 'Z:',
            [string]$FooFile  = 'foo.xml',
            [int]$N = 10
        )
    
        # Find the foo.xml files, pipe their paths to foreach-object
        Get-ChildItem -Filter $FooFile -Recurse -File -Name |ForEach-Object {
            # Find all sibling files (files in the same directory)
            # sort by when they were created and select only the first $N files
            Get-ChildItem -Path (Split-Path $_ -Parent) -File |Sort CreationTime -Descending |Select-Object -ExpandProperty FullName -First $N
        }
    }
    

    我认为最简单的方法是:

  • 查找整个树中的所有“foo.xml”
  • 在父目录中查找最新文件
  • 输出其
    FullName
    (其路径)属性
  • 这应该做到:

    funtion Get-NewestFooFileSibling {
        param(
            [string]$RootPath = 'Z:',
            [string]$FooFile  = 'foo.xml',
            [int]$N = 10
        )
    
        # Find the foo.xml files, pipe their paths to foreach-object
        Get-ChildItem -Filter $FooFile -Recurse -File -Name |ForEach-Object {
            # Find all sibling files (files in the same directory)
            # sort by when they were created and select only the first $N files
            Get-ChildItem -Path (Split-Path $_ -Parent) -File |Sort CreationTime -Descending |Select-Object -ExpandProperty FullName -First $N
        }
    }
    

    马西亚斯,非常感谢。我要试着衡量一下表现。马西亚斯,非常感谢。我们将尝试并测量性能。