0

I am trying to make a backup program, one of my first small projects to help me learn.

So far, I can get it to copy 1 file, but when I add a list of files, it does not copy.

The way it works is i click add files, each file path is added to my listbox, then click Select Destination, it adds the destination to a variable.

I am using code from here to copy files - Copy the entire contents of a directory in C#

It will copy ok when I add 1 path to the listbox, but when I add more than one, it basically joins my 2 path strings together and doesn't work.

Is there any way to make it copy 1 by one, or would I have too add the paths to an array, and then read them from it?

//============================
//  GLOBALSCOPE VARIABLES
//============================ 
string backUpDes;

public Form1()
{
    InitializeComponent();
    backUpLbl.Visible = false;
    backUpDesLbl.Visible = false;
}

//============================
//         BUTTONS
//============================
//
//ABOUT BUTTON
//
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
    string message = "Adam" + "\n\r" + "Version 1" + "\n\r" + "Backup Program" + "\n\r" + "n3z";
    string caption = "Program Information";
    MessageBoxButtons buttons = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Information;

    MessageBox.Show(message, caption, buttons, icon);
}
//
//NEW BACKUP BUTTON
//
private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = addFilesDia.ShowDialog();

    if(result == DialogResult.OK)
    {
        listBox1.Items.Add(addFilesDia.SelectedPath);
    }
    else
    {
        if(MessageBox.Show("Please select a folder to backup", "Select a Folder", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
        {
            addFilesDia.ShowDialog();
        }
    }
}
//
//SELECT DESTINATION FOLDER BUTTON
//
private void button1_Click_1(object sender, EventArgs e)
{
    DialogResult result = desDia.ShowDialog();

    if (result == DialogResult.OK)
    {
        backUpLbl.Visible = true;
        backUpDesLbl.Visible = true;
        backUpDesLbl.Text = desDia.SelectedPath;
        backUpDes = desDia.SelectedPath;
    }
    else
    {
        if (MessageBox.Show("Please Select a Destination Folder for your backup.", "Select a Destination", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
        {
            desDia.ShowDialog();
        }
    }
}
//
//START BACKUP
//
private void startBtn_Click(object sender, EventArgs e)
{
    MessageBox.Show(listBox1.Items.Count.ToString());      //For testing stage only

    string path = "";

    foreach (var item in listBox1.Items)
    {
        path += item.ToString();
    }

    //Now Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
        Directory.CreateDirectory(dirPath.Replace(path, backUpDes));

    //Copy all the files & Replaces any files with the same name
    foreach (string newPath in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
        File.Copy(newPath, newPath.Replace(path, backUpDes), true);

    textBox2.Text = path;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nezz
  • 41
  • 2
  • 11

2 Answers2

0

you are appending each Item with path string in the Event startBtn_Click

    foreach (var item in listBox1.Items)
    {
        path += item.ToString();
    }

try with

    foreach (var item in listBox1.Items)
    {
        path = item.ToString();

        //Now Create all of the directories
        foreach (string dirPath in Directory.GetDirectories(path, "*",
           SearchOption.AllDirectories))
           Directory.CreateDirectory(dirPath.Replace(path, backUpDes));

        //Copy all the files & Replaces any files with the same name
        foreach (string newPath in Directory.GetFiles(path, "*.*",
           SearchOption.AllDirectories))
           File.Copy(newPath, newPath.Replace(path, backUpDes), true);

    }
Roy
  • 69
  • 4
  • I addited my reply , can you try like this. Your copy file operations must be inside the foreach loop. – Roy Sep 04 '15 at 14:40
  • Thanks Roy, this way it only copies the last path. I am thinking about using an array which I will try and populate with the paths from the listbox, then pass the values from the array to the copy code somehow, though I have no idea how I would go about doing that. – Nezz Sep 04 '15 at 14:49
  • Are you sure you put all the copy logic inside the foreach loop? try to debug the code. – Roy Sep 04 '15 at 15:14
0

As far as i can work out, this is it fixed.

 string path = "";
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            path = listBox1.Items[i].ToString();
            textBox2.Text += path;

            //Now Create all of the directories
            foreach (string dirPath in Directory.GetDirectories(path, "*",
               SearchOption.AllDirectories))
                Directory.CreateDirectory(dirPath.Replace(path, backUpDes));

            //Copy all the files & Replaces any files with the same name
            foreach (string newPath in Directory.GetFiles(path, "*.*",
               SearchOption.AllDirectories))
                File.Copy(newPath, newPath.Replace(path, backUpDes), true);

            //OLD CODE
            //foreach (var item in listBox1.Items)
            //{
            //    path = item.ToString();

            //    //Now Create all of the directories
            //    foreach (string dirPath in Directory.GetDirectories(path, "*",
            //       SearchOption.AllDirectories))
            //        Directory.CreateDirectory(dirPath.Replace(path, backUpDes));

            //    //Copy all the files & Replaces any files with the same name
            //    foreach (string newPath in Directory.GetFiles(path, "*.*",
            //       SearchOption.AllDirectories))
            //        File.Copy(newPath, newPath.Replace(path, backUpDes), true);
            //}
        }
Nezz
  • 41
  • 2
  • 11