Click here to Skip to main content
14,427,893 members

Batch Renamer in C#

Rate this:
2.89 (13 votes)
Please Sign up or sign in to vote.
2.89 (13 votes)
26 Jan 2010CPOL
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

License

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

Share

About the Author

Mohammad Dayyan
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

Comments and Discussions

 
QuestionGreat tool but ask how to do specific job Pin
Asholino9-Jan-19 7:04
MemberAsholino9-Jan-19 7:04 
QuestionVery thanks to you Pin
DevMostafa17-Jun-13 5:32
MemberDevMostafa17-Jun-13 5:32 
General[My vote of 1] this should go to 'coding horrors'.. Pin
Seishin#27-Jan-10 5:06
MemberSeishin#27-Jan-10 5:06 
GeneralRe: [My vote of 1] this should go to 'coding horrors'.. Pin
Mohammad Dayyan27-Jan-10 5:46
MemberMohammad Dayyan27-Jan-10 5:46 
GeneralAnt Renamer and BackgroundWorker Pin
TobiasP10-Aug-08 3:46
MemberTobiasP10-Aug-08 3:46 
GeneralI always wanted Pin
vikas amin31-Jul-08 13:03
Membervikas amin31-Jul-08 13:03 
GeneralRe: I always wanted Pin
Mohammad Dayyan2-Aug-08 11:17
MemberMohammad Dayyan2-Aug-08 11: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.

Article
Posted 30 Jul 2008

Stats

34.6K views
1.5K downloads
28 bookmarked