Solved

c# copy todestination and continue rename

Posted on 2010-11-27
2
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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