Home > Software design >  Using Powershell, How do I filter each file in a subdirectories content, write out the filtered file
Using Powershell, How do I filter each file in a subdirectories content, write out the filtered file

Time:11-23

I have a subdirectory that each weeks I dump CSV files into. I wish to take the files, remove certain ones based on the content, and rewrite the file in a 'Filtered' Subdirectory. It has to be a one liner.

The closest I've come, provides the results as a single file. I would like to 'parse a csv, filter it, save it with a version of the original file name, then to the next file, an next file etc. The data is series of 8 small, 3 field CSV's.

(example) SOURCE CSV:

"Test","Tested","Total"
"2021-A-0123","12","20"
"2021-B-0125","15","19"
"2021-A-0123-S","10","11"
"2022-A-0126","10","11"

** CODE:**

Import-Csv -Path (Get-ChildItem -Path 'C:\Users\me\Documents\' -Filter '*.csv').FullName | where-object {$_."Test" -Match "^202[1,2]-A-[0-9]{4}$"} 

RESULTS

Test        Tested     Total
--------    ---------- -----
2021-A-0123 12         20   
2022-A-0126 10         11   
2021-A-0123 12         20   
2022-A-0126 10         11   
2021-A-0123 12         20   
2022-A-0126 10         11   
2021-A-0123 12         20   
2022-A-0126 10         11   
2021-A-0123 12         20   
2022-A-0126 10         11

CodePudding user response:

If I understood correctly, you just need to loop over each CSV and save each new version after being filtered.

Here you just need to define the initial folder and the destination folder, both variables defined at the top of the script. The code might be more verbose than really needed but I believe it's better this way so you can follow the thought process.

A one liner, in this case (and in most cases in my opinion), will be probably awful to read and harder to maintain if some else is reading your code.

$initialFolder = 'path/to/csvFolder'
$destinationFolder = 'path/to/filteredCsvFolder'

foreach($file in Get-ChildItem $initialFolder -Filter *.csv)
{
    $fileName = "{0} - FILTERED" -f $file.Name
    $exportPath = Join-Path $destinationFolder -ChildPath $fileName

    Import-Csv $file |
    Where-Object { $_.Test -Match "^202[1,2]-A-[0-9]{4}$" } |
    Export-Csv $exportPath -NoTypeInformation
}
  • Related