0

I have a move function which moves the files from one folder to another folder.. I am wondering how can i show the total numbers of files moved and moved filenames in a dialog box. or any idea..!!!!

Here the problem is, Once i press Move all the files moving, if at all there are some duplicate files it shows error msg in my grid view.. rest files starts keeps moving. SO i need a method once all files are moved.. i need to show a dialog box with total number of files moved and filenames moved in a dialog box.. is that possible.. or any idea.. Please post it..

I really appriciate that..

My codes:

 private void button3_Click(object sender, EventArgs e)
{
    update = false;
    this.Enabled = false;
    backgroundWorker2.RunWorkerAsync();
    move();
    backgroundWorker2.CancelAsync();
    System.Threading.Thread.Sleep(500);
    this.Enabled = true;
    update = true;
}

public void move()
{
    update = false;
    string archieve_path = ini.ReadValue("Location", "Archive");
    string release_path = ini.ReadValue("Location", "Release");
    string draft_path = ini.ReadValue("Location", "Draft");
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if ((!String.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString()) && String.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Release Path"].Value.ToString())) &&
            (dataGridView1.Rows[i].Cells[0].Value != null && (bool)dataGridView1.Rows[i].Cells[0].Value))
        {
            string draftpath = dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString();
            string relasepath = dataGridView1.Rows[i].Cells["Release Path"].Value.ToString();
            string archievepath = dataGridView1.Rows[i].Cells["Archive"].Value.ToString();
            string draftname = System.IO.Path.GetFileName(draftpath);
            if (relasepath != string.Empty && draftname.Equals(Path.GetFileName(relasepath), StringComparison.OrdinalIgnoreCase))
            {
                dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Release: " + draftname + "";
                dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                continue;
            }
            if (archievepath != string.Empty && draftname.Equals(Path.GetFileName(archievepath), StringComparison.OrdinalIgnoreCase))
            {
                dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Archive: " + draftname + "";
                dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                continue;
            }
            string newpath = System.IO.Path.Combine(release_path, draftname);
            System.IO.File.Move(draftpath, newpath);
            dataGridView1.Rows[i].Cells["Release Path"].Value = newpath;
            dataGridView1.Rows[i].Cells["Draft Path"].Value = string.Empty;
            dataGridView1.Rows[i].Cells[0].Value = false; //Checkbox
        }
        if ((!String.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString())
            && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Release Path"].Value.ToString())) &&
            (dataGridView1.Rows[i].Cells[0].Value != null && (bool)dataGridView1.Rows[i].Cells[0].Value))
        {
            string draftPath = dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString();
            string releasepath = dataGridView1.Rows[i].Cells["Release Path"].Value.ToString();
            string archievepath = dataGridView1.Rows[i].Cells["Archive"].Value.ToString();
            string draftname = System.IO.Path.GetFileName(draftPath);
            string archievename = System.IO.Path.GetFileName(archievepath);
            string releasename = System.IO.Path.GetFileName(releasepath);
            if (archievepath != string.Empty && draftname.Equals(Path.GetFileName(archievepath), StringComparison.OrdinalIgnoreCase))
            {
                dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Archive: " + draftname + "";
                dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                continue;
            }
            if (releasepath != string.Empty && draftname.Equals(Path.GetFileName(releasepath), StringComparison.OrdinalIgnoreCase))
            {
                dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Release: " + draftname + "";
                dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                continue;
            }
            if (archievepath != string.Empty && releasename.Equals(Path.GetFileName(archievepath), StringComparison.OrdinalIgnoreCase))
            {
                dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Archive: " + releasename + "";
                dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                continue;
            }
            string fName1 = System.IO.Path.GetFileNameWithoutExtension(draftPath);
            string fName2 = System.IO.Path.GetFileNameWithoutExtension(releasepath);
            string fName3 = System.IO.Path.GetFileNameWithoutExtension(archievepath);
            var f1 = GetValue(fName1.ToCharArray()[fName1.Length - 2]) * 16 + GetValue(fName1.ToCharArray()[fName1.Length - 1]);
            var f2 = GetValue(fName2.ToCharArray()[fName2.Length - 2]) * 16 + GetValue(fName2.ToCharArray()[fName2.Length - 1]);
            // var f3 = GetValue(fName3.ToCharArray()[fName3.Length - 2]) * 16 + GetValue(fName3.ToCharArray()[fName3.Length - 1]);
            if (f1 > f2)//till here
            {
                //moves from realse to archieve
                string newpath = System.IO.Path.Combine(archieve_path, releasename); //draft name + archieve
                System.IO.File.Move(releasepath, newpath);
                dataGridView1.Rows[i].Cells["Release Path"].Value = string.Empty;
            }
            if (f1 < f2)
            {
                //moves draft to archieve
                string newpath = System.IO.Path.Combine(archieve_path, draftname);
                System.IO.File.Move(draftPath, newpath);
                dataGridView1.Rows[i].Cells["Draft Path"].Value = string.Empty;
                return;
            }
            string newpath1 = System.IO.Path.Combine(release_path, draftname);
            System.IO.File.Move(draftPath, newpath1);
            dataGridView1.Rows[i].Cells["Release Path"].Value = newpath1;
            dataGridView1.Rows[i].Cells["Draft Path"].Value = string.Empty;
            dataGridView1.Rows[i].Cells[0].Value = false; //Checkbox
        }
    }
    update = true;
}

Added code:

 StringBuilder sbBody = new StringBuilder();
        List<string> lstFiles = new List<string>();

foreach (string file in Directory.GetFiles(newpath, "*.txt"))
                {
                    // msg.Attachments.Add(new System.Net.Mail.Attachment(file));
                    lstFiles.Add(file);
                }
  • 1
    just create a List at the start of the process, and add each filename to the list after you move each file in your "File.Move(draftPath, newpath1)". Calling the Count method on the list will give you the count. – failedprogramming Dec 24 '14 at 05:43

1 Answers1

0

I would have thought this was a dupe, but apparently it isn't. Anyway, here's the outline.

Add a ProgressChanged event to backgroundWorker2, and have it run move() as well (which will require rewriting the function to split out all the code that updates the UI for errors and such-like into ProgressChanged delegate calls). In the event handler, update the UI with the newly-modified count and filename data from the parameters.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • giving an error when i added the list `An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The directory name is invalid.` – Stacy Kebler Dec 24 '14 at 12:29