I have a folder, with subfolders full of csv files. For each file I would like to get only the third column and save it in another file.
Example:
filedata1.csv
aa,bb,cc
cat,horse,"dog, bird"
4th,33,first and second
filedata1column3.csv
cc
dog, bird
first and second
what I tried but it turns out wrong:
param ( [String] $Filename = '*.csv',
[String] $SourceDir = 'C:\Data\',
)
ForEach ($csvFile in Get-ChildItem -Path $SourceDir -Filter $Filename){
(Import-Csv $csvFile)
cut -d, -f 3 *.csv > *column3.csv
Use the intrinsic
psobjectproperty to determine each file's column names from the objects thatImport-Csvparses the CSV data into, and extract the third name.Then use member-access enumeration to extract all property values from the rows (objects), relying on PowerShell's ability to use expressions (variable references) as property names.
Use
Join-Pathin conjunction with-f, the format operator to determine a suitable output file path and name, based on the input file name.Note:
In Windows PowerShell,
>and>>create "Unicode" (UTF-16LE) files, whereas in PowerShell (Core) 7+ they create BOM-less UTF-8 files.If you need a different encoding, use
Set-ContentandAdd-Contentwith an-Encodingargument, but note that in Windows PowerShell you cannot create BOM-less UTF-8 files that way.If you do need BOM-less UTF-8 files in Windows PowerShell, use the workaround from this answer (use this instead of the
>and>>statements, and use"`r`n"instead of"`n"if you need Windows-format CRLF newlines):To put it all together in a solution that:
Place the code in a
.ps1file and invoke the latter (e.g../Script.ps1), optionally with arguments that override the defaults.