Beispiel #1
0
        private void btnProcess_Click(object sender, EventArgs e)
        {
            this.btnNewFile.Enabled = false;
            this.progBar.Value      = 0;
            string delimiterString = (ddDelimiter.SelectedItem != null) ? ddDelimiter.SelectedItem.ToString() : ddDelimiter.Text;

            char[]   delimiter  = Utility.GetDelimiter(delimiterString);
            string[] strindexes = txtIndexes.Text.Split(',');
            int[]    indexes    = new int[strindexes.Length];
            for (short i = 0; i < indexes.Length; i++)
            {
                if (!Int32.TryParse(strindexes[i], out indexes[i]))
                {
                    MessageBox.Show("Please ensure that all Indexes are Integers", "Invalid Indexes Values", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (rtbOutPutFile.Text.Length == 0)
            {
                MessageBox.Show("Please select an output file name", "Output needed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            TrimmerData tData = new TrimmerData(rtbInputFile.Text, rtbOutPutFile.Text, indexes, delimiter, chkKeepBlank.Checked);

            this.backgroundWorker1.RunWorkerAsync(tData);
        }
Beispiel #2
0
        private int ProcessSourceFile(TrimmerData tData, BackgroundWorker worker, DoWorkEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string        line;

            string[] delimited;
            int      currentLine = 0;
            int      lastLineEnd;

            //Count the # of lines
            int lineCount = 0;

            using (StreamReader sr = File.OpenText(tData.SourceFile))
            {
                while (sr.ReadLine() != null)
                {
                    lineCount++;
                }
            }
            int tenPercent = Convert.ToInt32(lineCount * 0.1);

            //Start processing the file
            using (StreamReader sr = File.OpenText(tData.SourceFile))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    currentLine++;
                    bool keepLine = false;
                    delimited   = line.Split(tData.Delimiter);
                    lastLineEnd = sb.Length;
                    for (short i = 0; i < tData.Indexes.Length; i++)
                    {
                        if (keepLine == false && delimited[tData.Indexes[i]].Trim().Length > 0)
                        {
                            keepLine = true;
                        }

                        sb.Append(delimited[tData.Indexes[i]] + tData.Delimiter[0].ToString());
                    }
                    if (keepLine)
                    {
                        sb.Length = sb.Length - 1;
                        sb.Append("\r\n");
                    }
                    else
                    {
                        sb.Length = lastLineEnd;
                    }
                    if (currentLine % tenPercent == 0)
                    {
                        worker.ReportProgress(currentLine);
                        File.AppendAllText(tData.DestinationFile, sb.ToString());
                        sb.Length   = 0;
                        lastLineEnd = 0;
                    }
                }
                File.AppendAllText(tData.DestinationFile, sb.ToString());
                sb.Length       = 0;
                tsslStatus.Text = "Complete. Lines Processed " + currentLine.ToString();
            }
            return(currentLine);
        }