How to filter and exclude files from gci?

Welcome Forums General PowerShell Q&A How to filter and exclude files from gci?

Viewing 9 reply threads
  • Author
    Posts
    • #224937
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      I am using the following:

      Get-ChildItem $CSV_Files_Path -Filter *.csv | foreach-object {...}
      

      There is one specific csv file however i wish to exclude. sure i can move it from that directory, but i’d have to do that everytime its replaced so i would like this to be dynamic…

      I know that we cannot combine exclude with filter, so how can i accomplish this?

      pseudocode:

      Get-ChildItem $CSV_Files_Path -Filter *.csv -Exclude Fact.csv | foreach-object {...}
      

      even better, i’d like to take this further with user input file exclusion.

      i.e.ย param($excludedfiles)

      $excludedfiles = 'C:\CSVFiles\Fact.csv, C:\CSVFiles\fileabc.csv'
      

      or even as flexible as:

      $excludedfiles = 'Fact.csv, fileabc.csv'
      

      pseudocode:

      $excludedfiles = $excludedfiles.Split('(.+?)(?:,|$)')
      Get-ChildItem $CSV_Files_Path -Filter *.csv -Exclude $excludedfiles | foreach-object {...}

      related

    • #224952
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP
    • #224973
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Hello Cataster,

      Would something like this not work?

      function csvfilter{
          Param([string]$path,[string[]]$exclude)
          gci "$path\*" *.csv -Exclude @($exclude | ForEach-Object {"*$_*"})
      }
      

      Called like

      csvfilter c:\temp
      
      Directory: C:\temp
      
      Mode LastWriteTime Length Name
      ---- ------------- ------ ----
      -a---- 4/17/2020 6:54 PM 287 AzureIP+Disk.Csv
      -a---- 4/17/2020 9:47 PM 52700071 hugecsv.csv
      -a---- 4/17/2020 9:52 PM 335 hugenoduplicates.csv
      -a---- 4/17/2020 9:24 PM 229 noduplicates.csv
      -a---- 4/17/2020 9:31 PM 528 source.csv
      -a---- 4/13/2020 11:11 PM 557 test.csv
      -a---- 4/29/2020 8:17 PM 362 testvalues.csv
      

      and then

      csvfilter c:\temp "source.csv","test.csv","noduplicates.csv"
      
      Directory: C:\temp
      
      Mode LastWriteTime Length Name
      ---- ------------- ------ ----
      -a---- 4/17/2020 6:54 PM 287 AzureIP+Disk.Csv
      -a---- 4/17/2020 9:47 PM 52700071 hugecsv.csv
      -a---- 4/29/2020 8:17 PM 362 testvalues.csv
      
    • #225027
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Would something like this not work?

      Just FYI: When you follow the tiny little link he posted at the end of his question you’ll notice he seems to already have a sattisfying answer. ๐Ÿ˜‰

    • #225075
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      Hello Cataster,

      Would something like this not work?

      PowerShell
      5 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 17.9048px; width: 7.20119px; left: 45px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      function csvfilter{
      Param([string]$path,[string[]]$exclude)
      gci “$path\*” *.csv Exclude @($exclude | ForEach-Object {“*$_*”})
      }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Called like

      PowerShell
      14 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 17.9048px; width: 7.20119px; left: 52px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      csvfilter c:\temp
      Directory: C:\temp
      Mode LastWriteTime Length Name
      —- ————- —— —-
      a—- 4/17/2020 6:54 PM 287 AzureIP+Disk.Csv
      a—- 4/17/2020 9:47 PM 52700071 hugecsv.csv
      a—- 4/17/2020 9:52 PM 335 hugenoduplicates.csv
      a—- 4/17/2020 9:24 PM 229 noduplicates.csv
      a—- 4/17/2020 9:31 PM 528 source.csv
      a—- 4/13/2020 11:11 PM 557 test.csv
      a—- 4/29/2020 8:17 PM 362 testvalues.csv
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      and then

      PowerShell
      10 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 17.9048px; width: 7.20119px; left: 52px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      csvfilter c:\temp “source.csv”,“test.csv”,“noduplicates.csv”
      Directory: C:\temp
      Mode LastWriteTime Length Name
      —- ————- —— —-
      a—- 4/17/2020 6:54 PM 287 AzureIP+Disk.Csv
      a—- 4/17/2020 9:47 PM 52700071 hugecsv.csv
      a—- 4/29/2020 8:17 PM 362 testvalues.csv
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      These work but this line is much simpler ๐Ÿ™‚

      get-childitem -path .\*.csv* -exclude file1.csv,file2.csv

      I didnt realize at first exclude can handle multiple values

    • #225081
      js
      Participant
      Topics: 29
      Replies: 810
      Points: 2,432
      Helping Hand
      Rank: Community Hero

      Yeah, what I said on stackoverflow. ๐Ÿ™‚

    • #225087
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Oh sorry I didn’t see that. But he figured it out anyhow… he didn’t need any of us. ๐Ÿ™‚

    • #225096
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      Would something like this not work?

      Just FYI: When you follow the tiny little link he posted at the end of his question youโ€™ll notice he seems to already have a sattisfying answer. 😉

      learned the protocol from last year ๐Ÿ™‚

    • #225102
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      Yeah, what I said on stackoverflow. 🙂

      oh hey nice to see you here js lol, thanks for the help on SO!

    • #225105
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      Oh sorry I didnโ€™t see that. But he figured it out anyhowโ€ฆ he didnโ€™t need any of us. 🙂

      aw dont say that, i appreciate the help here as well ๐Ÿ™‚

      this will help future powershellers anyways, we are pioneering their road to success starting with these posts here ๐Ÿ™‚

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