Skip to content

Instantly share code, notes, and snippets.

@mavericksevmont
Last active October 29, 2020 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavericksevmont/3b02879737778dc23ba4c5c3c7a8ee57 to your computer and use it in GitHub Desktop.
Save mavericksevmont/3b02879737778dc23ba4c5c3c7a8ee57 to your computer and use it in GitHub Desktop.
<# PowerShell Script to convert dbf file to csv (or almost anything else, really) without Excel.
Requires dBASE.NET.dll assembly: https://github.com/henck/dBASE.NET
This is a draft
#>
$Source = "$PSScriptRoot\da_md_wpa.dbf" # dbf file
$dll = "$PSScriptRoot\dBASE.NET.dll" # Assembly
$BaseName = (Get-Item $Source).BaseName
$Count = $null
$Assembly = [Reflection.Assembly]::LoadFile("$dll") # Load Assembly
$dbf = New-Object dBASE.NET.Dbf # Load class
$dbf.Read($Source) # Read dbf file's content
$Headers = $dbf.Fields.Name
$Records = $dbf.Records
$Data = $Records.Data
$ColNum = $Headers.Count
foreach ($Row in $Records) {
$Count++
Write-Progress -Status "Completed $Count out of $($Records.Count)" -Activity "Converting $Source to CSV" `
-PercentComplete ($Count/$Records.count*100)
$obj = New-Object PSOBject
for ($i=0;$i -lt $ColNum;$i++){
$obj | Add-Member -MemberType NoteProperty -Name $Headers[$i] -Value $Row[$i] }
$Results = [array]$Results + $obj
$obj = $null
}
$Results | Export-Csv "$PSScriptRoot\$BaseName.csv" -NoTypeInformation -Force -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment