?
Solved

c# copy file reorder

Posted on 2010-11-28
3
Medium Priority
?
362 Views
Last Modified: 2012-05-10
the code below is work i use to copy mp3 file form sorce directory to destination directory ( some song is name in Thai language some mp3 player not accept so I change name of file to number)

how can I make a copy randomly copy every file from source but reorder

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
            {


                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
3 Comments
 
LVL 13

Expert Comment

by:Pryrates
ID: 34225347
ordering as shown in the explorer is done by windows itself.
If you want to rename each file to archive a certain order this is another thing.

I think you need to be a little more specific about what you want to archive and how "ordering" is meant.
0
 

Author Comment

by:teera
ID: 34225358
Hi Pryrates
 
      i have radio station and I have limit song that i can open due to copyright  my program is open song so i want to  rename the song to number so I can open with mp3 machine without problem but if i open every day with out reorder song it will not good  
    if program can reorder song randomly I will very hapy
0
 
LVL 13

Accepted Solution

by:
Pryrates earned 2000 total points
ID: 34226195
ah... now I get it.
Its is not about coping, its about renaming files to get a random order based on the std. sorting of your os.
If a simple prefix to the filenames would fit your needs maybe this might do
private void RandomizeFilesInDirectory(string src_directory)
        {
            string[] directoryFiles = Directory.GetFiles(src_directory);
            int fileCnt = directoryFiles.Length;
            string numberFormat = GetNumberFormatString(fileCnt);

            // first determine random prefix number 
            string[] randomNumbers = new string[fileCnt];
            Random random = new Random();
            for (int i = 0; i < fileCnt; i++)
            {
                bool next = false;
                while (!next)
                {
                    string prefix = random.Next(1, fileCnt + 1).ToString(numberFormat);
                    if (!randomNumbers.Contains(prefix))
                    {
                        randomNumbers[i] = prefix;
                        next = true;
                    }
                }
            }

            // now we rename the files
            for (int i = 0; i < fileCnt; i++)
            {
                FileInfo fi = new FileInfo(directoryFiles[i]);
                string newFileName = "";
                if (fi.Name.Contains("$"))
                {
                    newFileName = fi.Name.Substring(fi.Name.IndexOf("$") + 1);
                }
                else
                {
                    newFileName = fi.Name;
                }
                newFileName = string.Concat(randomNumbers[i], "$", newFileName);
                fi.MoveTo(Path.Combine(fi.Directory.FullName, newFileName));
            }
        }

//helper method
        private string GetNumberFormatString(int ref_number)
        {
            if (ref_number < 10)
                return "0";
            if (ref_number < 100)
                return "00";
            if (ref_number < 1000)
                return "000";
            if (ref_number < 10000)
                return "0000";
            if (ref_number < 100000)
                return "00000";

            return "000000";
        }

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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