I need to exclude certain folders DURING the process of searching certain files

Welcome Forums General PowerShell Q&A I need to exclude certain folders DURING the process of searching certain files

Viewing 2 reply threads
  • Author
    Posts
    • #261401
      Participant
      Topics: 6
      Replies: 6
      Points: 26
      Rank: Member

      Hi,

      The task appears to be a simple one but I’m out of ideas.

      – start Get-ChildItem -Recurse -File -Filter '.ini'
      – as soon as the ini file is found, stop processing the ini folder
      D:\Data\Fol1\fol2\fol3\123.ini > D:\Data\Fol1\fol2\fol3\ excluded
      D:\Data\Fol1\fol2\fol3\fol4\2.ini should not be included in results
      – continue to search for other directories
      – finish and return array of objects

      An alternative that I know would be using c# yield return but I would like to achieve this via PS 5.1.

    • #261881
      Participant
      Topics: 1
      Replies: 342
      Points: 326
      Helping Hand
      Rank: Contributor

      Does it matter if you continue the search but filter the results?  You could use Select-Object to return just the first result for each subfolder but I realise this isn’t ideal if, for performance reasons, you don’t want to search all subfolders.

      $folders = Get-ChildItem -Directory

      foreach ($folder in $folders) {

      Get-ChildItem $folder -Recurse -File -Filter *.ini | Select-Object -First 1

      }

      • This reply was modified 2 weeks, 4 days ago by Matt Bloomfield. Reason: Formatting
      • This reply was modified 2 weeks, 4 days ago by Matt Bloomfield. Reason: Formatting
    • #262223
      Participant
      Topics: 16
      Replies: 1794
      Points: 3,300
      Helping Hand
      Rank: Community Hero

      Another import aspect of this is finding it based off of what sort? Name, DateTime, etc.? You’re typically not searching per se, you are filtering a dataset to only return what you want. As Matt eluded, you can use Select-Object -First to find the newest txt file in a directory like so:

Viewing 2 reply threads
  • You must be logged in to reply to this topic.