Solved

c# copy todestination and continue rename

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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