Solved

C# copy file and recopy again

Posted on 2010-11-27
7
337 Views
Last Modified: 2012-05-10
the code below i try to copy form songs from sorce to destination foder  such as if sorce folder have 10 song and destination folder have 35 song I want to copy form source folder 35 song by recopy from begining song again until 35 song
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;



namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog dlg = new FolderBrowserDialog();

            if (dlg.ShowDialog() == DialogResult.OK)

            {

                sorrceTextbox.Text = dlg.SelectedPath;

            }





        }





        private void textBox1_TextChanged(object sender, EventArgs e)

        {



        }



        private void button2_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog dlg2 = new FolderBrowserDialog();

            if (dlg2.ShowDialog() == DialogResult.OK)

            {

                destTextbox.Text = dlg2.SelectedPath;

            }



        }



        private void textBox2_TextChanged(object sender, EventArgs e)

        {



        }



        private void startcopy_Click(object sender, EventArgs e)

        {



            

            if (String.IsNullOrEmpty(sorrceTextbox.Text) || String.IsNullOrEmpty(destTextbox.Text))

            {

                this.labelStart.Text = "Hello World";

            }

            else

            {

                int fileCount = Directory.GetFiles(destTextbox.Text).Length;

                this.labelStart.Text = fileCount.ToString() ;



                CopyFiles(sorrceTextbox.Text, destTextbox.Text);









            }







        }





        private void label1_Click(object sender, EventArgs e)

        {

            

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }

        

        public void CopyFiles(string sourceDir, string targetDir)

        {

            string[] sourceFiles = Directory.GetFiles(sourceDir);

            for (int i = 0; i < sourceFiles.Length; i++)

            {

                string sourceFile = sourceFiles[i];

                string targetFile = targetDir + "\\" + "1" + (i + 10).ToString().PadLeft(3, '0') + "1" + ".mp3";

                File.Copy(sourceFile, targetFile, true);

            }

        }





    }

}

Open in new window

0
Comment
Question by:teera
  • 4
  • 3
7 Comments
 
LVL 13

Expert Comment

by:Pryrates
ID: 34221512
sorry, but I am not sure if I get  your problem right.
Can you be more specific?

Do you want to sync the content of two folders?
Folder 1 has 10 files.
Folder 2 has 35 files.

After copy Folder 1 and 2 both have 45 files?
Or just folder 2 should contain 45 files after copy?
0
 

Author Comment

by:teera
ID: 34221663
hi   Pryrates

source            Folder 1 has 10 files.
target             Folder 2 has 35 files.


total is            60       folder will recopy until 35

it like a festival that have a song that compose for this festival and it mix with popular song but song that compose for festival will play many time

               
0
 
LVL 13

Expert Comment

by:Pryrates
ID: 34221695
what do you mean by "recopy until 35" ?

In your destination folder you have 35 files / items.
In your source folder you have 10 files / items.

You want in your destination folder to be as many copies of your source folder files as items are in your destination folder originally?
what to do:
a.) 35 destination, 10 in source => copy 10 files 35 times in your destination folder?
b.) 35 destination, 10 in source => 70 files should be in destination afterwards. 2 times all from source and 1st 5 from source to reach 35 copies.

Or did I still not get it?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:teera
ID: 34222103
Hi Pryrates

    if destination have 80  and source have 10 the total in destination is 160 toal  
    if the destination is 120 total will be 240
   
   it mean that sorce file will copy   1,2,3 ... 10  then 1 ,2 ... 10 .1,2 ..10 antil sorce destination was copy 120 file but change the name
0
 
LVL 13

Accepted Solution

by:
Pryrates earned 500 total points
ID: 34222908
now I get it
how should the files be named that are copied into the destination directory?
the sample is pretty much straight forward...

I wrote two methods. One to be called directly and one to find a free filename if destination file already exists.

just call "DoubleFileAmount" directly. You have to pass the source and the destination directory as parameters.

Hope this is what you were looking for :)
 
//function to be called by other code
        private void DoubleFileAmount(string source_folder, string destination_folder)
        {
            if (!Directory.Exists(source_folder))
                throw new Exception("Sourcefolder does not exist");

            if (!Directory.Exists(destination_folder))
                throw new Exception("Destinationfolder does not exist");

            if(Directory.GetFiles(source_folder).Length == 0)
                throw new Exception("Sourcefolder is empty");

            if (Directory.GetFiles(destination_folder).Length == 0)
                throw new Exception("Destinationfolder is empty");


            List<string> sourceFiles =  Directory.GetFiles(source_folder).ToList();
            int destinationFileAmount = Directory.GetFiles(destination_folder).Length;

            int fullCopies = (int)Math.Floor((decimal)destinationFileAmount / (decimal)sourceFiles.Count);
            int fillUps = destinationFileAmount % sourceFiles.Count;

            for (int i = 1; i <= fullCopies; i++)
            {
                foreach (string sourceFile in sourceFiles)
                {
                    FileInfo fi = new FileInfo(sourceFile);
                    string destination = Path.Combine(destination_folder, fi.Name.Replace(fi.Extension, string.Concat("_", i.ToString("000"), fi.Extension)));
                    if (File.Exists(destination))
                        destination = FreeFileName(destination, i.ToString("000"));

                    File.Copy(sourceFile, destination);
                }
            }

            fullCopies++;
            for(int i = 1; i <= fillUps; i++)
            {
                FileInfo fi = new FileInfo(sourceFiles[i-1]);

                string destination = Path.Combine(destination_folder, fi.Name.Replace(fi.Extension, string.Concat("_", fullCopies.ToString("000"), fi.Extension)));
                if (File.Exists(destination))
                    destination = FreeFileName(destination, fullCopies.ToString("000"));

                File.Copy(fi.FullName, destination);
            }
        }

        //internal function
        private string FreeFileName(string source, string marker)
        { 
            string fileName = source;
            while (File.Exists(fileName))
            {
                FileInfo fi = new FileInfo(source);
                fileName = Path.Combine(fi.Directory.FullName, fi.Name.Replace(fi.Extension, string.Concat("_", marker, fi.Extension)));
            }
            return fileName;
        }

Open in new window

0
 

Author Closing Comment

by:teera
ID: 34223984
thank you
0
 
LVL 13

Expert Comment

by:Pryrates
ID: 34224425
you are welcome
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now