Skip to content

Instantly share code, notes, and snippets.

@potatoqualitee
Last active August 29, 2015 14:13
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 potatoqualitee/ec046a1995aa406eba15 to your computer and use it in GitHub Desktop.
Save potatoqualitee/ec046a1995aa406eba15 to your computer and use it in GitHub Desktop.
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 1.
$csvfile = "C:\perf\bigperf.csv"
$csvfile = "C:\perf\tab.csv"
$csvdelimiter = "`t" # `t for tab
$firstrowcolumnnames = $false
Write-Output "Script started..."
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
$csv = New-Object Microsoft.VisualBasic.FileIO.TextFieldParser($csvfile)
$csv.TextFieldType = "Delimited"
$csv.SetDelimiters($csvdelimiter)
$csv.CommentTokens = "#"
$datatable = New-Object System.Data.DataTable
Write-Warning "Filling datatable"
$columns = (Get-Content $csvfile -First 1).Split($csvdelimiter)
foreach ($column in $columns) {
if ($firstrowcolumnnames -eq $true) {
[void]$datatable.Columns.Add($column)
} else { [void]$datatable.Columns.Add()}
}
while (!$csv.EndOfData) {$null = $datatable.Rows.Add($csv.ReadFields())}
if ($firstrowcolumnnames -eq $true) { $datatable.Rows.Remove($datatable.Rows.Item(0)) }
$totaltime = [math]::Round($elapsed.Elapsed.TotalSeconds,2)
Write-Host "Total Elapsed Time: $totaltime seconds. $($datatable.rows.count) rows added." -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment