I'm struggling trying to pass a variable not only throughta pipeline but a foreach.
I have in multiples directories a lot of csv files (some with the same name) adding some data to it.
I want to generate a unique csv file merging all files with same name but with one conditions:
- Add an extra column with the date from the above folder I picked up the data
I'm just missing how to pass the variable $outfile to the end of the pipeline (to the Export-CSV function).
$mainPath="C:\\....";
$outFilePath = "C:\\Users....\\";
$dateFolder="";
$inputDelimiter=';'
$outputDelimiter=','
$filter="*.csv";
$outFile = "";
Get-ChildItem $mainPath -Recurse -Include $filter `
| %{
$dateFolder = (Split-Path (Split-Path $_.DirectoryName -Parent )-Leaf)
$outfile = Split-Path $_ -Leaf
Import-Csv $_.FullName -Delimiter $inputDelimiter
} `
| Select-Object *,@{Name='Date'; Expression={"$dateFolder"}} `
| Export-Csv $outFilePath$outFile -NoTypeInformation -Delimiter $outputDelimiter
Thanks. Regards. Jose.