Click here to Skip to main content
Licence CPOL
First Posted 30 Jul 2008
Views 11,454
Downloads 295
Bookmarked 27 times

Batch Renamer in C#

By Mohammad Dayyan | 26 Jan 2010
Renames files
4 votes, 30.8%
1
3 votes, 23.1%
2
2 votes, 15.4%
3

4
4 votes, 30.8%
5
2.89/5 - 13 votes
μ 2.89, σa 2.96 [?]
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)

About the Author

Mohammad Dayyan



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 1] this should go to 'coding horrors'.. PinmemberSeishin#5:06 27 Jan '10  
Thread tr = new Thread(new ThreadStart(chnTarget));
and then
this.Invoke(new ThreadStart(ChangeName));
LOL.. read some articles about threading before you write something like this..
 

foreach (string filename in FullPathFileNames)
        {
            if (cancel) break;
 
            progress_value++;
maybe for loop would do better?!..
 
File.Move(filename, new_filename);
you don't check if the file new_filename exists - prone to do some damage with this random trick of yours..
 
 
try{
[...]
Application.DoEvents();
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
you know what finally block is for, right?
 
seriously, this code is horror.. for articles like this 'poor' (1) is a too high rating..
 
life is study!!!

GeneralRe: [My vote of 1] this should go to 'coding horrors'.. Pinmember_Mohammad_5:46 27 Jan '10  
GeneralAnt Renamer and BackgroundWorker PinmemberTobiasP3:46 10 Aug '08  
GeneralI always wanted Pinmembervikas amin13:03 31 Jul '08  
GeneralRe: I always wanted PinmemberMohammad Dayyan11:17 2 Aug '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 27 Jan 2010
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid