In a PowerShell script, I read a CSV file.
I have to "fix" some values. Specifically, the CSV may contains either an empty value, either literally NULL
or sometimes -
. All these values are to be considered as $null
.
Is there a way to intercept CSV parsing to handle that?
Actually I have a working solution, but the solution is awfully slow. Iterating over 2500 items takes 20 minutes, while reading as is the CSV file takes only a few seconds.
The idea is to iterate over each properties :
$private:result = @{}
foreach($private:prop in $private:line.PSObject.Properties){
$private:value = $null
$private:result.Add($private:prop.Name, ($private:value | Filter-Value))
}
$private:result
...
function Filter-Value{
param(
[Parameter(Position=0, ValueFromPipeline=$true)]
[object]$In
)
if(-not $In){
$null
}
elseif(($In -is [string]) -and ($In.Length -eq 0)) {
$null
}
elseif(($In -eq "NULL") -or ($In -eq "-")) {
$null
}
else{
$In
}
}
Full code :
function Import-CsvEx{
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[string]$Path,
[Parameter()]
[string]$Delimiter
)
begin{
Write-Verbose "Begin read of file $Path"
}
process{
# We use file stream and stream reader to automatically detect encoding
$private:fileStream = [System.IO.File]::OpenRead($Path)
$private:streamReader = New-Object System.IO.StreamReader($private:fileStream, [System.Text.Encoding]::Default, $true)
$private:fileContent = $private:streamReader.ReadToEnd()
$private:streamReader.Dispose()
$private:fileStream.Dispose()
$private:csv = ConvertFrom-Csv $private:fileContent -Delimiter $Delimiter
for($private:i=0; $private:i -lt $private:csv.Count ; $private:i++){
Write-Progress -Id 1003 -Activity "Reading CSV" -PercentComplete ($private:i*100/$private:csv.count)
$private:line = $private:csv[$private:i]
$private:result = @{}
foreach($private:prop in $private:line.PSObject.Properties){
$private:value = $null
$private:result.Add($private:prop.Name, ($private:value | Filter-Value))
}
# actually outputs the object to the pipeline
New-Object psobject -Property $private:result
}
Write-Progress -Id 1003 -Activity "Reading CSV" -Completed
}
end{
Write-Verbose "End read of file $Path"
}
}
function Filter-Value{
param(
[Parameter(Position=0, ValueFromPipeline=$true)]
[object]$In
)
if(-not $In){
$null
}
elseif(($In -is [string]) -and ($In.Length -eq 0)) {
$null
}
elseif(($In -eq "NULL") -or ($In -eq "-")) {
$null
}
else{
$In
}
}