Cataster
Cataster

Reputation: 3541

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 {...}

Upvotes: 0

Views: 666

Answers (1)

js2010
js2010

Reputation: 27516

There's a lot of annoying things with -filter, -include, and -exclude in powershell 5. It's resolved in newer versions. You could combine -path and -exclude:

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

Upvotes: 1

Related Questions