Solved

c# copy todestination and continue rename

Posted on 2010-11-27
2
206 Views
Last Modified: 2012-05-10
the code be low if i want to copy from source folder to destination folder  3 time (I want to play mp3 files in a loop)

  may be i need to run

    CopyFiles(sorrceTextbox.Text, destTextbox.Text
 
   but how can i  continue number of file name in destination directory
   suppose  when it copy finish in the first time it finish with 10051 then next it continue  to 10061

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
  • 2
2 Comments
 
LVL 13

Expert Comment

by:Pryrates
ID: 34225181
you can only archive that goal if you have strict naming conventions for your destination files.
You need to isolate a naming - pattern. Therefore you need to analyse the files already stored in the destination directory to find the last file that has been copied to the directory.
How are your files named in your destination directory? Is it random or are those files following a naming convention as well?
F.e. are ONLY the copied files start with "1" ?
0
 
LVL 13

Accepted Solution

by:
Pryrates earned 500 total points
ID: 34225268
maybe this might help
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 = FreeFileNamePattern(targetDir);                

                File.Copy(sourceFile, targetFile, true);

            }

}





        private string FreeFileNamePattern(string source)

        {

            string fileName = Path.Combine(source, "10011.mp3");



            int i = 2;

            while (File.Exists(fileName))

            {

                fileName = Path.Combine(source, string.Concat("1", i.ToString("000"),"1.mp3"));

                i++;

            }

            return fileName;

        }

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

11 Experts available now in Live!

Get 1:1 Help Now