I cobbled together an acceptable method to split large txt/csv files with headers. The original script merely opened a file select dialog, then ran the process - 600+ meg files with 185 columns were easily split in 4 seconds. Then I decided to add a dialog to allow the user to proceed or cancel. Cancel works fine, the button to proceed just errors.... I'm doing something fundamentally wrong, just haven't figured it out
#split test - with timer Function File_Split($filename, $Form, $TextBox1) { $sw = new-object System.Diagnostics.Stopwatch $sw.Start() $f1 = (Get-ItemProperty $filename).BaseName $ext = (Get-ItemProperty $filename).Extension $ext = $ext.Replace(".","") $rootName = Split-Path -Path $filename $rootName = $rootName + "\" $f1 = $rootName + $f1 + "_" $header = (Get-Content $filename | Select-Object -First 1) #$lines = 0; switch -File $filename { default { ++$lines } } #will add more time to process #$linesperFile = $lines/2 #roughly divide file in half $linesperFile = 250000 $filecount = 1 $reader = $null try{ $reader = [io.file]::OpenText($filename) try{ $TextBox1.text = “Creating file number $filecount” $writer = [io.file]::CreateText(“{0}{1}.{2}” -f ($f1,$filecount.ToString(“000”),$ext)) $linecount = 0 while($reader.EndOfStream -ne $true) { $TextBox1.text = “Reading $linesperFile” If ( $filecount -ge 2 ) { $writer.WriteLine($header); } while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)){ $writer.WriteLine($reader.ReadLine()); $linecount++ } $filecount++ if($reader.EndOfStream -ne $true) { $TextBox1.text = “Closing file” $writer.Dispose(); “Creating file number $filecount” $writer = [io.file]::CreateText(“{0}{1}.{2}” -f ($f1,$filecount.ToString(“000”),$ext)) $linecount = 0 } } } finally { $writer.Dispose(); } } finally { $reader.Dispose(); } $sw.Stop(); $TextBox1.text = “Split complete in ” + $sw.Elapsed.TotalSeconds + “seconds” } $openFileDialog = New-Object windows.forms.openfiledialog $openFileDialog.initialDirectory = [System.IO.Directory]::GetCurrentDirectory() $openFileDialog.title = "Select File to Split for CDL Upload" $openFileDialog.filter = "Text Files|*.txt*|CSV Files|*.csv*|All Files|*.*" $openFileDialog.ShowHelp = $True #Write-Host "Select Downloaded Settings File. (Minimize this window to see FileOpen Dialog)" -ForegroundColor Green $result = $openFileDialog.ShowDialog() # Display the Dialog / Wait for user response #$result if($result -eq "OK") { $filename = $OpenFileDialog.filename } else { Write-Host "File Selection Cancelled!" -ForegroundColor Yellow Return} Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = New-Object System.Drawing.Point(381,148) $Form.text = "Split File" $Form.TopMost = $false $Form.add_Load($Form_Load) $TextBox1 = New-Object system.Windows.Forms.TextBox $TextBox1.multiline = $false $TextBox1.text = "$filename" $TextBox1.width = 357 $TextBox1.height = 35 $TextBox1.Anchor = 'top,right,bottom,left' $TextBox1.location = New-Object System.Drawing.Point(10,20) $TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "Run Split Process" $Button1.width = 131 $Button1.height = 45 $Button1.Anchor = 'top,right,bottom,left' $Button1.location = New-Object System.Drawing.Point(15,51) $Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Button1.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#7ed321") $Button2 = New-Object system.Windows.Forms.Button $Button2.text = "Cancel" $Button2.width = 122 $Button2.height = 45 $Button2.Anchor = 'top,right,bottom,left' $Button2.location = New-Object System.Drawing.Point(242,53) $Button2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Button2.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#d0021b") $Form.controls.AddRange(@($TextBox1,$Button1,$Button2)) $Button1.Add_Click({File_Split($filename, $Form, $TextBox1)}) $Button2.Add_Click({$Form.Add_FormClosing({$_.Cancel=$false});$Form.Close()}) [void] $Form.ShowDialog() return
Fixed it. Just eliminated the function, put the code in click event and added a short delay before closing dialog. Works well,