How to filter and exclude files from gci?

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

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

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. :wink:

[quote quote=224973]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
[/quote] 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

Yeah, what I said on stackoverflow. :slight_smile:

Oh sorry I didn’t see that. But he figured it out anyhow… he didn’t need any of us. :slight_smile:

learned the protocol from last year :slight_smile:

[quote quote=225081]Yeah, what I said on stackoverflow. 🙂

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

[quote quote=225087]Oh sorry I didn’t see that. But he figured it out anyhow… he didn’t need any of us. 🙂

[/quote]
aw dont say that, i appreciate the help here as well :slight_smile:

this will help future powershellers anyways, we are pioneering their road to success starting with these posts here :slight_smile: