Permalink
Browse files

More and more work...

  • Loading branch information...
1 parent 748eeee commit b4202fe8582c1c91bd98171fc0e4df4553f71da7 @Valien committed Nov 2, 2012

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -14,7 +14,7 @@ namespace CSVSplitter
{
public partial class CSVSplitterForm : Form
{
-
+
public CSVSplitterForm()
{
InitializeComponent();
@@ -23,8 +23,8 @@ public CSVSplitterForm()
}
public delegate void UpdateProgressSub(int CurrentLine);
- private Boolean _IsAbort;
OpenFileDialog OFD = new OpenFileDialog();
+ string OutputFolder;
// Count lines in CSV
static int CountCSVLines(string f)
@@ -50,50 +50,48 @@ private void browse_Button_Click(object sender, EventArgs e)
{
csv_TextBox.Text = OFD.FileName.ToString();
// counting how many lines are in the CSV file initially
- countResult = CountCSVLines(OFD.FileName.ToString()).ToString("N3");
+ countResult = CountCSVLines(OFD.FileName.ToString()).ToString("N0");
lblStatus.Text = "There are " + countResult + " lines in this CSV file.";
}
}
-
+
// Start button
private void splitNow_Button_Click(object sender, EventArgs e)
{
- //System.Threading.Thread th = new System.Threading.Thread(SplitIt);
- //th.Start();
- //SplitIt();
+
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
- //SplitIt();
splitNow_Button.Enabled = false;
+ cancel_Button.Enabled = true;
}
-
+
}
-
+
// Cancel button
private void cancel_Button_Click(object sender, EventArgs e)
{
- //_IsAbort = true;
- //this.Close();
+
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
backgroundWorker1.CancelAsync();
splitNow_Button.Enabled = true;
-
}
}
private void SplitCSV(string FilePath, int LineCount, int MaxOutputFile)
//private void SplitCSV(string FilePath, int LineCount, int MaxOutputFile, Action<int> UpdateProgress, bool IsAbort)
{
+ //BackgroundWorker worker = as BackgroundWorker;
+
// Validate first
- if (LineCount < 100) throw new Exception("Number of lines must be more than 100.");
+ //if (LineCount < 100) throw new Exception("Number of lines must be more than 100.");
// Open the CSV file for reading
StreamReader Reader = new StreamReader(FilePath);
// Create the output directory
- string OutputFolder = FilePath + "_Pieces";
+ OutputFolder = FilePath + "_Pieces";
if (Directory.Exists(FilePath) == false)
{
Directory.CreateDirectory(OutputFolder);
@@ -109,10 +107,6 @@ private void SplitCSV(string FilePath, int LineCount, int MaxOutputFile)
{
// Update progress
FileIndex += 1;
- //if ((UpdateProgress != null))
- //{
- // UpdateProgress.Invoke((FileIndex - 1) * LineCount);
- //}
// Check if the number of split files do not exceed the limit
if ((MaxOutputFile < FileIndex) & (MaxOutputFile > 0))
@@ -127,19 +121,20 @@ private void SplitCSV(string FilePath, int LineCount, int MaxOutputFile)
Writer.AutoFlush = false;
Writer.WriteLine(strHeader);
-
// Read and write precise number of rows
for (int i = 0; i < LineCount; i++)
{
string s = Reader.ReadLine();
- if (s != null) //& (_IsAbort = false))
+ if (s != null)
{
Writer.WriteLine(s);
+ //worker.ReportProgress(i * 10);
}
else
{
Writer.Flush();
//Writer.Close();
+
break;
}
}
@@ -149,48 +144,26 @@ private void SplitCSV(string FilePath, int LineCount, int MaxOutputFile)
Writer.Close();
}
+
+ //lblStatus.Text = LineCount + " and " + countResult;
} while (true);
Reader.Close();
}
- public void SplitIt()
- {
-
- //splitNow_Button.Enabled = false;
- //cancel_Button.Enabled = true;
-
- // Kick it off!
- try
- {
- //Split SplitCSV = new Split();
- //SplitCSV(csv_TextBox.Text, nol_NumericUpDown.Value, maxPieces_NumericUpDown.Value, UpdateProgress, _IsAbort);
-
- SplitCSV(csv_TextBox.Text, (int)nol_NumericUpDown.Value, (int)maxPieces_NumericUpDown.Value);
-
- //if (!_IsAbort)
- //{
- // MessageBox.Show("Completed Successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
- //}
- //else
- //{
- // _IsAbort = false;
- // MessageBox.Show("Split process aborted by user.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- //}
-
- } // end try
- catch (Exception e)
- {
- MessageBox.Show("Unable to split the CSV file. Reason: " + e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
-
- // finally
- // {
- //splitNow_Button.Enabled = true;
- //cancel_Button.Enabled = false;
- // }
-
- }
+ //public void SplitIt()
+ //{
+ // // Kick it off!
+ // try
+ // {
+ // //SplitCSV(csv_TextBox.Text, nol_NumericUpDown.Value, maxPieces_NumericUpDown.Value, UpdateProgress, _IsAbort);
+ // SplitCSV(csv_TextBox.Text, (int)nol_NumericUpDown.Value, (int)maxPieces_NumericUpDown.Value);
+ // } // end try
+ // catch (Exception e)
+ // {
+ // MessageBox.Show("Unable to split the CSV file. Reason: " + e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+ // }
+ //}
//public void UpdateProgress(int CurrentLine)
//{
@@ -200,43 +173,56 @@ public void SplitIt()
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
- for (int i = 0; i < countResult.Length; i++)//CountCSVLines(OFD.FileName.ToString())
+ for (int i = 0; i < maxPieces_NumericUpDown.Value; i++)//CountCSVLines(OFD.FileName.ToString())
{
-
-
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
- SplitIt();
- worker.ReportProgress(i * 10);
+ //SplitIt();
+ try
+ {
+ SplitCSV(csv_TextBox.Text, (int)nol_NumericUpDown.Value, (int)maxPieces_NumericUpDown.Value);
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Unable to split the CSV file. Reason: " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+ //throw;
+ }
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
- lblStatus.Text = (e.ProgressPercentage.ToString() + "%");
- progressBar1.Value = e.ProgressPercentage;
- progressBar1.Update();
+
+ // few ideas to show the progress -- count total # of lines and return progress based on each block of lines completed
+ // count current progress of lines and show progress that way.
+ // take - linecount / total lines (50K/135K) * 100 = 37%) -- return that result?
+
+ //lblStatus.Text = (e.ProgressPercentage.ToString() + "%");
+
+ // progressBar1.Value = e.ProgressPercentage;
+ //progressBar1.Update();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
lblStatus.Text = "Split Canceled.";
+ cancel_Button.Enabled = false;
}
else if (e.Error != null)
{
lblStatus.Text = "Error: " + e.Error.Message;
}
else
{
- lblStatus.Text = "Split complete!";
+ lblStatus.Text = "Split complete! Your files are located here: " + OutputFolder;
splitNow_Button.Enabled = true;
}

0 comments on commit b4202fe

Please sign in to comment.