Click here to Skip to main content
15,894,180 members
Articles / Programming Languages / C#

Batch Renamer in C#

Rate me:
Please Sign up or sign in to vote.
2.89/5 (13 votes)
26 Jan 2010CPOL 41K   1.8K   28   7
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:

C#
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.

C#
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:

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat tool but ask how to do specific job Pin
Asholino9-Jan-19 6:04
Asholino9-Jan-19 6:04 
QuestionVery thanks to you Pin
DevMostafa17-Jun-13 4:32
DevMostafa17-Jun-13 4:32 
General[My vote of 1] this should go to 'coding horrors'.. Pin
Seishin#27-Jan-10 4:06
Seishin#27-Jan-10 4:06 
GeneralRe: [My vote of 1] this should go to 'coding horrors'.. Pin
Mohammad Dayyan27-Jan-10 4:46
Mohammad Dayyan27-Jan-10 4:46 
GeneralAnt Renamer and BackgroundWorker Pin
TobiasP10-Aug-08 2:46
TobiasP10-Aug-08 2:46 
I suppose the article works as an introduction to basic file operations in .NET, but:

- If one needs a program to rename a lot of files, Ant Renamer[^] offers much more functionality.
- Rather than using the Thread class, this type of operation is exactly what the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker</a>[<a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" target="_blank" title="New Window">^</a>] class is made for.
GeneralI always wanted Pin
vikas amin31-Jul-08 12:03
vikas amin31-Jul-08 12:03 
GeneralRe: I always wanted Pin
Mohammad Dayyan2-Aug-08 10:17
Mohammad Dayyan2-Aug-08 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.