Filtering files by partial name match

后端 未结 1 539
深忆病人
深忆病人 2020-12-07 05:47

I have a network share with 20.000 XML files in the format

username-computername.xml

There are duplicate entries in the form of (when a user received a n

相关标签:
1条回答
  • 2020-12-07 06:33

    You can use Group-Object to group files by a custom attribute:

    $xmlfiles | Group-Object { $_.Name.Split('-')[0] }
    

    The above statement will produce a result like this:

    Count Name    Group
    ----- ----    -----
        1 BLRPPR  {BLRPPR-SKB52084.xml}
        1 BLRSIA  {BLRSIA-SKB50871.xml}
        2 S028DS  {S028DS-SKB51334.xml, s028ds-SKB52424.xml}
        2 S02FL6  {S02FL6-SKB51644.xml, S02FL6-SKB52197.xml}
        1 S02VUD  {S02VUD-SKB52083.xml}

    where the Group property contains the original FileInfo objects.

    Expand the groups in a ForEach-Object loop, sort each group by LastWriteTime, and select the most recent file from it:

    ... | ForEach-Object {
      $_.Group | Sort-Object LastWriteTime -Desc | Select-Object -First 1
    }
    
    0 讨论(0)
提交回复
热议问题