Click here to Skip to main content
Email Password   helpLost your password?
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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
General[My vote of 1] this should go to 'coding horrors'..
Seishin#
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'..
_Mohammad_
5:46 27 Jan '10  
Isn't better to guide me ?
However I'll read more about Threading
GeneralAnt Renamer and BackgroundWorker
TobiasP
3:46 10 Aug '08  
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 "http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker["http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" target="_blank" title="New Window">^] class is made for.
GeneralI always wanted
vikas amin
13:03 31 Jul '08  
good work , I like the mass rename idea & always wanted it .

Vikas Amin
My First Article on CP" Virtual Serail Port "[^]
modified on Thursday, July 24, 2008 5:33 PM

GeneralRe: I always wanted
Mohammad Dayyan
11:17 2 Aug '08  
You're welcome.


Last Updated 27 Jan 2010 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010