Solved

c# copy todestination and continue rename

Posted on 2010-11-27
2
211 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

827 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