简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM