65.9K
CodeProject is changing. Read more.
Home

Batch Renamer in C#

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.89/5 (13 votes)

Jul 30, 2008

CPOL
viewsIcon

41746

downloadIcon

1762

Renames files

BatchRenamer

BatchRenamer

Introduction

In this project, you will familiarize yourself with Random and Directory and File classes and a little with the Thread class.

Background

There were some applications that did this work like FileRen.

Using the Code

For batch renaming, first we must get the user's folder name:

private void ChooseFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                SelectedPathText.Text = folderBrowserDialog1.SelectedPath;
            }
        }

Second, we must get all of the file names into the above folder. For this, I used the Thread class.

private void rename_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure ?",
                 "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                Thread tr = new Thread(new ThreadStart(chnTarget));
                tr.Start();
            }
        }
private void chnTarget()
{
    this.Invoke(new ThreadStart(ChangeName));
}

Then I wrote the ChangeName method:

private void ChangeName()
{
    Random rand = new Random(DateTime.Now.Second);
    string folder_path = SelectedPathText.Text;
    long progress_value = 0;
    string new_filename = "";
    cancel = false;

    progressBar1.Value = 0;

    try
    {
        string[] FullPathFileNames = System.IO.Directory.GetFiles(folder_path);

        progressBar1.Maximum = FullPathFileNames.Length;
        if (FullPathFileNames.Length == 0) throw new Exception("No File Found \n");

        foreach (string filename in FullPathFileNames)
        {
            if (cancel) break;

            progress_value++;

            try
            {
                if (!radioRandom.Checked)
                {
                    new_filename = folder_path + "\\" +
                        textBoxTemplate.Text +
                        progress_value +
                        Path.GetExtension(filename); //gets extension of a file
                }
                else
                {
                    if (checkBoxOmitExtension.Checked)
                        new_filename = folder_path + "\\" + rand.Next();
                    else
                        new_filename = folder_path + "\\" + rand.Next() +
                                    Path.GetExtension(filename);
                }

                File.Move(filename, new_filename);
                Application.DoEvents();
            }
            catch (Exception)
            {
                Application.DoEvents();
            }

            progressBar1.Value = (int)progress_value;
            progressBar1.Update();
        }

        SelectedPathText.Text = "Complete";
        cancel = false;
    }
    catch (Exception ex)
    {
        SelectedPathText.Text = ex.Message;
    }
}

That's all.

History

  • 30th July, 2008: Initial post
  • 12th January, 2010: Fixed a bug