Reputation: 2101
I am working in PowerShell. My PSVersion is 3.0. I have a folder with several CSV files in it. Each file can have several columns in it, and contains column headers on the first line. The order of the columns will not always be the same. I need to fetch only certain columns out of each file. The columns I need to fetch are the same across all files, and all files will have at least the columns I need to fetch (and generally more). Once fetched, I need to export the columns fetched for each file out to a corresponding CSV, in a different folder. For example, I have the following 2 files in my input folder:
file1.csv
:
Col1 Col2 Col3 Col4 a 1 z 0.0
file2.csv
:
Col4 Col1 Col3 Col2 1.0 b x 2
I need to fetch only columns Col1
and Col4
from each file. So, I expect to output the following files:
file1.csv
:
Col1 Col4 a 0.0
file2.csv
:
Col1 Col4 b 1.0
Below is my script:
$inputDirectory = 'C:\some_path\input\';
$outputDirectory = 'C:\another_path\output\';
$inputFiles = Get-ChildItem -Path $inputDirectory -Recurse -Include *.csv;
ForEach-Object {
Import-Csv $inputFiles |
Select-Object -Property Col1, Col4 |
Export-Csv $outputDirectory + $inputFiles.Name -NoTypeInformation
}
I am having trouble with the Export-Csv
command. I keep getting the following error:
A positional parameter cannot be found that accepts argument 'System.Object{}'
So, something about my piping in of Select-Object
is wrong. How can I resolve this error and achieve my goal?
Upvotes: 2
Views: 137
Reputation: 19684
Your logic is sound, but your syntax doesn't make sense.
$Path = 'C:\Temp\Input'
$Output = 'C:\Temp\Output'
$Files = Get-ChildItem -Path $Path -Recurse -Include '*.csv'
ForEach ($File in $Files)
{
Import-Csv -Path $File.FullName | Select-Object -Property @('Col1','Col4') |
Export-Csv -Path (Join-Path $Output $File.Name) -NoTypeInformation -Append
}
Upvotes: 2