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: 41
      Replies: 113
      Points: 647
      Rank: Major Contributor

      I am using the following:

      [/crayon]

      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:

      [/crayon]

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

      i.e.ย  param($excludedfiles)

      [/crayon]

      or even as flexible as:

      [/crayon]

      pseudocode:

      [/crayon]

      related

    • #224952
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP
    • #224973
      Participant
      Topics: 8
      Replies: 562
      Points: 2,149
      Helping Hand
      Rank: Community Hero

      Hello Cataster,

      Would something like this not work?

      Called like

      and then

    • #225027
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      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: 41
      Replies: 113
      Points: 647
      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: 32
      Replies: 842
      Points: 2,688
      Helping Hand
      Rank: Community Hero

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

    • #225087
      Participant
      Topics: 8
      Replies: 562
      Points: 2,149
      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: 41
      Replies: 113
      Points: 647
      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: 41
      Replies: 113
      Points: 647
      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: 41
      Replies: 113
      Points: 647
      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
  • The topic ‘How to filter and exclude files from gci?’ is closed to new replies.